[PATCH] D119226: [clang][lex][minimizer] Avoid treating path separators as comments

Jan Svoboda via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 8 02:12:22 PST 2022


jansvoboda11 created this revision.
jansvoboda11 added reviewers: Bigcheese, arphaman.
jansvoboda11 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

The minimizer strips out single-line comments (introduced by `//`). This sequence of characters can also appear in `#include` or `#import` directives where they play the role of path separators. We already avoid stripping this character sequence for `#include` but not for `#import` (which has the same semantics). This patch makes it so `#import <A//A.h>` is not affected by minimization. Previously, we would incorrectly reduce it into `#import <A`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D119226

Files:
  clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp
  clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp


Index: clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp
===================================================================
--- clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp
+++ clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp
@@ -453,6 +453,14 @@
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#include <A>\n", Out));
   EXPECT_STREQ("#include <A>\n", Out.data());
 
+  ASSERT_FALSE(
+      minimizeSourceToDependencyDirectives("#include <A//A.h>\n", Out));
+  EXPECT_STREQ("#include <A//A.h>\n", Out.data());
+
+  ASSERT_FALSE(
+      minimizeSourceToDependencyDirectives("#include \"A//A.h\"\n", Out));
+  EXPECT_STREQ("#include \"A//A.h\"\n", Out.data());
+
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives("#include_next <A>\n", Out));
   EXPECT_STREQ("#include_next <A>\n", Out.data());
@@ -460,6 +468,13 @@
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#import <A>\n", Out));
   EXPECT_STREQ("#import <A>\n", Out.data());
 
+  ASSERT_FALSE(minimizeSourceToDependencyDirectives("#import <A//A.h>\n", Out));
+  EXPECT_STREQ("#import <A//A.h>\n", Out.data());
+
+  ASSERT_FALSE(
+      minimizeSourceToDependencyDirectives("#import \"A//A.h\"\n", Out));
+  EXPECT_STREQ("#import \"A//A.h\"\n", Out.data());
+
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives("#__include_macros <A>\n", Out));
   EXPECT_STREQ("#__include_macros <A>\n", Out.data());
Index: clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp
===================================================================
--- clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp
+++ clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp
@@ -392,7 +392,7 @@
     do {
       // Iterate over strings correctly to avoid comments and newlines.
       if (*Last == '"' || *Last == '\'' ||
-          (*Last == '<' && top() == pp_include)) {
+          (*Last == '<' && (top() == pp_include || top() == pp_import))) {
         if (LLVM_UNLIKELY(isRawStringLiteral(First, Last)))
           skipRawString(Last, End);
         else


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119226.406740.patch
Type: text/x-patch
Size: 2071 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220208/bae82efa/attachment.bin>


More information about the cfe-commits mailing list