[PATCH] D82103: [analyzer] Remove forbidden characters from a SourceLocation filename for a graph dump on Windows

Denys Petrov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 19 15:46:43 PDT 2020


ASDenysPetrov updated this revision to Diff 272204.
ASDenysPetrov added a comment.

Removed accidentally included unrelated changes.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D82103/new/

https://reviews.llvm.org/D82103

Files:
  clang/include/clang/Basic/JsonSupport.h
  clang/test/Analysis/exploded-graph-rewriter/win_path_forbidden_chars.cpp


Index: clang/test/Analysis/exploded-graph-rewriter/win_path_forbidden_chars.cpp
===================================================================
--- /dev/null
+++ clang/test/Analysis/exploded-graph-rewriter/win_path_forbidden_chars.cpp
@@ -0,0 +1,26 @@
+// 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 %t.dot | 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.
+
+extern void __assert_fail(__const char *__assertion, __const char *__file,
+                          unsigned int __line, __const char *__function)
+    __attribute__((__noreturn__));
+#define assert(expr) \
+  ((expr) ? (void)(0) : __assert_fail(#expr, __FILE__, __LINE__, __func__))
+
+int test(int *array, int size) {
+  //Here `assert` helps to produce angle brackets.
+  assert(size);
+  return array[size];
+}
+
+//This test is passed if exploded_graph_rewriter handles dot file without errors.
+// CHECK: digraph "ExplodedGraph"
Index: clang/include/clang/Basic/JsonSupport.h
===================================================================
--- clang/include/clang/Basic/JsonSupport.h
+++ 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 @@
     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()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82103.272204.patch
Type: text/x-patch
Size: 2469 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200619/aed05096/attachment.bin>


More information about the cfe-commits mailing list