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


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
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to