[clang] be9c581 - [analyzer] Remove forbidden characters from a filename for a graph dump on Windows
Denys Petrov via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 22 07:27:28 PDT 2020
Author: Denys Petrov
Date: 2020-06-22T17:27:20+03:00
New Revision: be9c5818351bf99a193edbc82e4dc25ab5206a44
URL: https://github.com/llvm/llvm-project/commit/be9c5818351bf99a193edbc82e4dc25ab5206a44
DIFF: https://github.com/llvm/llvm-project/commit/be9c5818351bf99a193edbc82e4dc25ab5206a44.diff
LOG: [analyzer] Remove forbidden characters from a filename for a graph dump on Windows
Summary:
Windows forbidden file path characters are used in a field `file`, while creating a dump `dot` file using an argument -analyzer-dump-egraph. It specifically relates to angle brackets when using `<scratch space>`, `<built-in>`, `<command line>` values in filenames. It causes that script exploded-graph-rewriter.py incorrectly parses the dump.
Fix:
Remove forbidden characters from filename for Windows platform, when creating graph dump file.
Differential Revision: https://reviews.llvm.org/D82103
Added:
clang/test/Analysis/exploded-graph-rewriter/win_path_forbidden_chars.cpp
Modified:
clang/include/clang/Basic/JsonSupport.h
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/JsonSupport.h b/clang/include/clang/Basic/JsonSupport.h
index 1b118b45a049..8b02e440df44 100644
--- a/clang/include/clang/Basic/JsonSupport.h
+++ b/clang/include/clang/Basic/JsonSupport.h
@@ -13,7 +13,7 @@
#include "clang/Basic/SourceManager.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
-
+#include <iterator>
namespace clang {
@@ -98,7 +98,16 @@ inline void printSourceLocationAsJson(raw_ostream &Out, SourceLocation Loc,
if (AddBraces)
Out << "{ ";
std::string filename(PLoc.getFilename());
-#ifdef _WIN32 // Handle windows-specific path delimiters.
+#ifdef _WIN32
+ // Remove forbidden Windows path characters
+ auto RemoveIt =
+ std::remove_if(filename.begin(), filename.end(), [](auto Char) {
+ static const char ForbiddenChars[] = "<>*?\"|";
+ return std::find(std::begin(ForbiddenChars), std::end(ForbiddenChars),
+ Char) != std::end(ForbiddenChars);
+ });
+ filename.erase(RemoveIt, filename.end());
+ // Handle windows-specific path delimiters.
std::replace(filename.begin(), filename.end(), '\\', '/');
#endif
Out << "\"line\": " << PLoc.getLine()
diff --git a/clang/test/Analysis/exploded-graph-rewriter/win_path_forbidden_chars.cpp b/clang/test/Analysis/exploded-graph-rewriter/win_path_forbidden_chars.cpp
new file mode 100644
index 000000000000..4eac964a4f44
--- /dev/null
+++ b/clang/test/Analysis/exploded-graph-rewriter/win_path_forbidden_chars.cpp
@@ -0,0 +1,20 @@
+// FIXME: Figure out how to use %clang_analyze_cc1 with our lit.local.cfg.
+// RUN: %clang_cc1 -analyze -triple x86_64-unknown-linux-gnu \
+// RUN: -analyzer-checker=core \
+// RUN: -analyzer-dump-egraph=%t.dot %s
+// RUN: %exploded_graph_rewriter --verbose %t.dot 2>&1 | FileCheck %s
+// REQUIRES: asserts
+// UNSUPPORTED: !windows
+
+// Angle brackets shall not be presented in the field `file`,
+// because exploded_graph_rewriter handles it as a file path
+// and such symbols are forbidden on Windows platform.
+
+void test() {
+ // This produces angle brackets.
+ char text[] = __FILE__;
+}
+
+// This test is passed if exploded_graph_rewriter handles dot file without errors.
+// CHECK: DEBUG:root:Line: digraph "Exploded Graph"
+// CHECK: \"file\": \"scratch space\"
More information about the cfe-commits
mailing list