From 2eba3db1cb3fee5615454cb39b37eace37e20706 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Fri, 19 Jan 2024 13:30:23 +0000 Subject: [PATCH 1/4] C++: Add a test for SuccessfullyExtractedFiles.ql and friends. --- .../Diagnostics/ExtractionErrors.expected | 2 ++ .../Diagnostics/ExtractionErrors.qlref | 1 + .../Diagnostics/ExtractionWarnings.expected | 2 ++ .../Diagnostics/ExtractionWarnings.qlref | 1 + .../FailedExtractorInvocations.expected | 0 .../FailedExtractorInvocations.qlref | 1 + .../test/query-tests/Diagnostics/Info.expected | 6 ++++++ cpp/ql/test/query-tests/Diagnostics/Info.ql | 17 +++++++++++++++++ .../SuccessullyExtractedFiles.expected | 4 ++++ .../Diagnostics/SuccessullyExtractedFiles.qlref | 1 + .../query-tests/Diagnostics/containserror.cpp | 5 +++++ .../query-tests/Diagnostics/containswarning.cpp | 4 ++++ .../query-tests/Diagnostics/doesnotcompile.cpp | 5 +++++ cpp/ql/test/query-tests/Diagnostics/header.h | 2 ++ .../test/query-tests/Diagnostics/successful.cpp | 5 +++++ 15 files changed, 56 insertions(+) create mode 100644 cpp/ql/test/query-tests/Diagnostics/ExtractionErrors.expected create mode 100644 cpp/ql/test/query-tests/Diagnostics/ExtractionErrors.qlref create mode 100644 cpp/ql/test/query-tests/Diagnostics/ExtractionWarnings.expected create mode 100644 cpp/ql/test/query-tests/Diagnostics/ExtractionWarnings.qlref create mode 100644 cpp/ql/test/query-tests/Diagnostics/FailedExtractorInvocations.expected create mode 100644 cpp/ql/test/query-tests/Diagnostics/FailedExtractorInvocations.qlref create mode 100644 cpp/ql/test/query-tests/Diagnostics/Info.expected create mode 100644 cpp/ql/test/query-tests/Diagnostics/Info.ql create mode 100644 cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.expected create mode 100644 cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.qlref create mode 100644 cpp/ql/test/query-tests/Diagnostics/containserror.cpp create mode 100644 cpp/ql/test/query-tests/Diagnostics/containswarning.cpp create mode 100644 cpp/ql/test/query-tests/Diagnostics/doesnotcompile.cpp create mode 100644 cpp/ql/test/query-tests/Diagnostics/header.h create mode 100644 cpp/ql/test/query-tests/Diagnostics/successful.cpp diff --git a/cpp/ql/test/query-tests/Diagnostics/ExtractionErrors.expected b/cpp/ql/test/query-tests/Diagnostics/ExtractionErrors.expected new file mode 100644 index 000000000000..f7d1b79cd17a --- /dev/null +++ b/cpp/ql/test/query-tests/Diagnostics/ExtractionErrors.expected @@ -0,0 +1,2 @@ +| doesnotcompile.cpp:4:2:4:2 | Recoverable extraction error: identifier 'This' is undefined | Extraction failed in doesnotcompile.cpp with error "doesnotcompile.cpp", line 4: error: identifier "This" is undefined\n \tThis is not correct C/C++ code.\n \t^\n\n | 2 | +| doesnotcompile.cpp:4:10:4:10 | Recoverable extraction error: expected a ';' | Extraction failed in doesnotcompile.cpp with error "doesnotcompile.cpp", line 4: error: expected a ";"\n \tThis is not correct C/C++ code.\n \t ^\n\n | 2 | diff --git a/cpp/ql/test/query-tests/Diagnostics/ExtractionErrors.qlref b/cpp/ql/test/query-tests/Diagnostics/ExtractionErrors.qlref new file mode 100644 index 000000000000..1bf951f18994 --- /dev/null +++ b/cpp/ql/test/query-tests/Diagnostics/ExtractionErrors.qlref @@ -0,0 +1 @@ +Diagnostics/Internal/ExtractionErrors.ql diff --git a/cpp/ql/test/query-tests/Diagnostics/ExtractionWarnings.expected b/cpp/ql/test/query-tests/Diagnostics/ExtractionWarnings.expected new file mode 100644 index 000000000000..69cba1e1420c --- /dev/null +++ b/cpp/ql/test/query-tests/Diagnostics/ExtractionWarnings.expected @@ -0,0 +1,2 @@ +| doesnotcompile.cpp:4:2:4:2 | Recoverable extraction error: identifier 'This' is undefined | Extraction failed in doesnotcompile.cpp with warning "doesnotcompile.cpp", line 4: error: identifier "This" is undefined\n \tThis is not correct C/C++ code.\n \t^\n\n | 1 | +| doesnotcompile.cpp:4:10:4:10 | Recoverable extraction error: expected a ';' | Extraction failed in doesnotcompile.cpp with warning "doesnotcompile.cpp", line 4: error: expected a ";"\n \tThis is not correct C/C++ code.\n \t ^\n\n | 1 | diff --git a/cpp/ql/test/query-tests/Diagnostics/ExtractionWarnings.qlref b/cpp/ql/test/query-tests/Diagnostics/ExtractionWarnings.qlref new file mode 100644 index 000000000000..2df3d933e8ae --- /dev/null +++ b/cpp/ql/test/query-tests/Diagnostics/ExtractionWarnings.qlref @@ -0,0 +1 @@ +Diagnostics/ExtractionWarnings.ql diff --git a/cpp/ql/test/query-tests/Diagnostics/FailedExtractorInvocations.expected b/cpp/ql/test/query-tests/Diagnostics/FailedExtractorInvocations.expected new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/cpp/ql/test/query-tests/Diagnostics/FailedExtractorInvocations.qlref b/cpp/ql/test/query-tests/Diagnostics/FailedExtractorInvocations.qlref new file mode 100644 index 000000000000..e3f6cd687d3d --- /dev/null +++ b/cpp/ql/test/query-tests/Diagnostics/FailedExtractorInvocations.qlref @@ -0,0 +1 @@ +Diagnostics/FailedExtractorInvocations.ql diff --git a/cpp/ql/test/query-tests/Diagnostics/Info.expected b/cpp/ql/test/query-tests/Diagnostics/Info.expected new file mode 100644 index 000000000000..55e3310fd19b --- /dev/null +++ b/cpp/ql/test/query-tests/Diagnostics/Info.expected @@ -0,0 +1,6 @@ +| containserror.cpp:0:0:0:0 | containserror.cpp | query-tests/Diagnostics/containserror.cpp | fromSource, normalTermination | +| containswarning.cpp:0:0:0:0 | containswarning.cpp | query-tests/Diagnostics/containswarning.cpp | fromSource, normalTermination | +| doesnotcompile.cpp:0:0:0:0 | doesnotcompile.cpp | query-tests/Diagnostics/doesnotcompile.cpp | ExtractionProblem (severity 1), fromSource, normalTermination | +| file://:0:0:0:0 | | | | +| header.h:0:0:0:0 | header.h | query-tests/Diagnostics/header.h | fromSource | +| successful.cpp:0:0:0:0 | successful.cpp | query-tests/Diagnostics/successful.cpp | fromSource, normalTermination | diff --git a/cpp/ql/test/query-tests/Diagnostics/Info.ql b/cpp/ql/test/query-tests/Diagnostics/Info.ql new file mode 100644 index 000000000000..6acad213f50b --- /dev/null +++ b/cpp/ql/test/query-tests/Diagnostics/Info.ql @@ -0,0 +1,17 @@ +import cpp +import Diagnostics.ExtractionProblems + +string describe(File f) { + exists(ExtractionProblem e | e.getFile() = f | + result = "ExtractionProblem (severity " + e.getSeverity().toString() + ")" + ) + or + f.fromSource() and result = "fromSource" + or + exists(Compilation c | c.getAFileCompiled() = f | + (c.normalTermination() and result = "normalTermination") + ) +} + +from File f +select f, concat(f.getRelativePath(), ", "), concat(describe(f), ", ") diff --git a/cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.expected b/cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.expected new file mode 100644 index 000000000000..2c2f7e9d14a9 --- /dev/null +++ b/cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.expected @@ -0,0 +1,4 @@ +| containserror.cpp:0:0:0:0 | containserror.cpp | File successfully extracted. | +| containswarning.cpp:0:0:0:0 | containswarning.cpp | File successfully extracted. | +| header.h:0:0:0:0 | header.h | File successfully extracted. | +| successful.cpp:0:0:0:0 | successful.cpp | File successfully extracted. | diff --git a/cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.qlref b/cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.qlref new file mode 100644 index 000000000000..e67b23fd5576 --- /dev/null +++ b/cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.qlref @@ -0,0 +1 @@ +Diagnostics/SuccessfullyExtractedFiles.ql diff --git a/cpp/ql/test/query-tests/Diagnostics/containserror.cpp b/cpp/ql/test/query-tests/Diagnostics/containserror.cpp new file mode 100644 index 000000000000..175fcc579f11 --- /dev/null +++ b/cpp/ql/test/query-tests/Diagnostics/containserror.cpp @@ -0,0 +1,5 @@ +// semmle-extractor-options: --expect_errors + +void containserror() { + #error An error! +} diff --git a/cpp/ql/test/query-tests/Diagnostics/containswarning.cpp b/cpp/ql/test/query-tests/Diagnostics/containswarning.cpp new file mode 100644 index 000000000000..9ab8674404fd --- /dev/null +++ b/cpp/ql/test/query-tests/Diagnostics/containswarning.cpp @@ -0,0 +1,4 @@ + +void containswarning() { + #warning A warning. +} diff --git a/cpp/ql/test/query-tests/Diagnostics/doesnotcompile.cpp b/cpp/ql/test/query-tests/Diagnostics/doesnotcompile.cpp new file mode 100644 index 000000000000..dfe8dad9caa9 --- /dev/null +++ b/cpp/ql/test/query-tests/Diagnostics/doesnotcompile.cpp @@ -0,0 +1,5 @@ +// semmle-extractor-options: --expect_errors + +void doesnotcompile() { + This is not correct C/C++ code. +} diff --git a/cpp/ql/test/query-tests/Diagnostics/header.h b/cpp/ql/test/query-tests/Diagnostics/header.h new file mode 100644 index 000000000000..cf4fafd829ea --- /dev/null +++ b/cpp/ql/test/query-tests/Diagnostics/header.h @@ -0,0 +1,2 @@ + +// a header file diff --git a/cpp/ql/test/query-tests/Diagnostics/successful.cpp b/cpp/ql/test/query-tests/Diagnostics/successful.cpp new file mode 100644 index 000000000000..6c59b87c45a7 --- /dev/null +++ b/cpp/ql/test/query-tests/Diagnostics/successful.cpp @@ -0,0 +1,5 @@ + +#include "header.h" + +void successful() { +} From 4691bf2cb50dd7c98eda97e596b825dfccb45160 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Fri, 19 Jan 2024 14:35:56 +0000 Subject: [PATCH 2/4] C++: Be more optimistic about successfully scanned files. --- cpp/ql/src/Diagnostics/SuccessfullyExtractedFiles.ql | 9 +++------ .../Diagnostics/SuccessullyExtractedFiles.expected | 1 + 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/cpp/ql/src/Diagnostics/SuccessfullyExtractedFiles.ql b/cpp/ql/src/Diagnostics/SuccessfullyExtractedFiles.ql index d3768c36106d..6bd804a6e1d8 100644 --- a/cpp/ql/src/Diagnostics/SuccessfullyExtractedFiles.ql +++ b/cpp/ql/src/Diagnostics/SuccessfullyExtractedFiles.ql @@ -1,16 +1,13 @@ /** - * @name Successfully extracted files - * @description Lists all files in the source code directory that were extracted without encountering a problem in the file. + * @name Extracted files + * @description Lists all files in the source code directory that were extracted. * @kind diagnostic * @id cpp/diagnostics/successfully-extracted-files * @tags successfully-extracted-files */ import cpp -import ExtractionProblems from File f -where - not exists(ExtractionProblem e | e.getFile() = f) and - exists(f.getRelativePath()) +where exists(f.getRelativePath()) and f.fromSource() select f, "File successfully extracted." diff --git a/cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.expected b/cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.expected index 2c2f7e9d14a9..e677078e428e 100644 --- a/cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.expected +++ b/cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.expected @@ -1,4 +1,5 @@ | containserror.cpp:0:0:0:0 | containserror.cpp | File successfully extracted. | | containswarning.cpp:0:0:0:0 | containswarning.cpp | File successfully extracted. | +| doesnotcompile.cpp:0:0:0:0 | doesnotcompile.cpp | File successfully extracted. | | header.h:0:0:0:0 | header.h | File successfully extracted. | | successful.cpp:0:0:0:0 | successful.cpp | File successfully extracted. | From 01ee61e5ea0ec45b4bf4fd89f2ca0336288d3076 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Fri, 19 Jan 2024 14:46:19 +0000 Subject: [PATCH 3/4] C++: Change note. --- cpp/ql/src/change-notes/2024-01-19-extracted-files.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 cpp/ql/src/change-notes/2024-01-19-extracted-files.md diff --git a/cpp/ql/src/change-notes/2024-01-19-extracted-files.md b/cpp/ql/src/change-notes/2024-01-19-extracted-files.md new file mode 100644 index 000000000000..df6de1576ace --- /dev/null +++ b/cpp/ql/src/change-notes/2024-01-19-extracted-files.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* The diagnostic query `cpp/diagnostics/successfully-extracted-files` now considers any C/C++ file seen during extraction, even one with some errors, to be extracted / scanned. This affects the Code Scanning UI measure of scanned C/C++ files. From 5127542677e30529ca673c43b8bf73e4f400daec Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Fri, 19 Jan 2024 16:31:34 +0000 Subject: [PATCH 4/4] C++: Rename the query file. --- .../{SuccessfullyExtractedFiles.ql => ExtractedFiles.ql} | 0 ...uccessullyExtractedFiles.expected => ExtractedFiles.expected} | 0 cpp/ql/test/query-tests/Diagnostics/ExtractedFiles.qlref | 1 + .../test/query-tests/Diagnostics/SuccessullyExtractedFiles.qlref | 1 - 4 files changed, 1 insertion(+), 1 deletion(-) rename cpp/ql/src/Diagnostics/{SuccessfullyExtractedFiles.ql => ExtractedFiles.ql} (100%) rename cpp/ql/test/query-tests/Diagnostics/{SuccessullyExtractedFiles.expected => ExtractedFiles.expected} (100%) create mode 100644 cpp/ql/test/query-tests/Diagnostics/ExtractedFiles.qlref delete mode 100644 cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.qlref diff --git a/cpp/ql/src/Diagnostics/SuccessfullyExtractedFiles.ql b/cpp/ql/src/Diagnostics/ExtractedFiles.ql similarity index 100% rename from cpp/ql/src/Diagnostics/SuccessfullyExtractedFiles.ql rename to cpp/ql/src/Diagnostics/ExtractedFiles.ql diff --git a/cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.expected b/cpp/ql/test/query-tests/Diagnostics/ExtractedFiles.expected similarity index 100% rename from cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.expected rename to cpp/ql/test/query-tests/Diagnostics/ExtractedFiles.expected diff --git a/cpp/ql/test/query-tests/Diagnostics/ExtractedFiles.qlref b/cpp/ql/test/query-tests/Diagnostics/ExtractedFiles.qlref new file mode 100644 index 000000000000..e900e9c5314b --- /dev/null +++ b/cpp/ql/test/query-tests/Diagnostics/ExtractedFiles.qlref @@ -0,0 +1 @@ +Diagnostics/ExtractedFiles.ql diff --git a/cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.qlref b/cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.qlref deleted file mode 100644 index e67b23fd5576..000000000000 --- a/cpp/ql/test/query-tests/Diagnostics/SuccessullyExtractedFiles.qlref +++ /dev/null @@ -1 +0,0 @@ -Diagnostics/SuccessfullyExtractedFiles.ql