[clang] d8298f0 - [clang][lex][minimizer] Avoid treating path separators as comments

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 15 00:50:51 PST 2022


Author: Jan Svoboda
Date: 2022-02-15T09:49:19+01:00
New Revision: d8298f04a9681fcbb16d7fc4872690b5504a0d52

URL: https://github.com/llvm/llvm-project/commit/d8298f04a9681fcbb16d7fc4872690b5504a0d52
DIFF: https://github.com/llvm/llvm-project/commit/d8298f04a9681fcbb16d7fc4872690b5504a0d52.diff

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

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`.

Reviewed By: arphaman

Differential Revision: https://reviews.llvm.org/D119226

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp b/clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp
index d247e1471cc8..10536438e2fc 100644
--- a/clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp
+++ b/clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp
@@ -401,7 +401,7 @@ void Minimizer::printToNewline(const char *&First, const char *const End) {
     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

diff  --git a/clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp b/clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp
index 5fd4f6024bff..f6ef96eaed5c 100644
--- a/clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp
+++ b/clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp
@@ -453,6 +453,14 @@ TEST(MinimizeSourceToDependencyDirectivesTest, Include) {
   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 @@ TEST(MinimizeSourceToDependencyDirectivesTest, Include) {
   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());


        


More information about the cfe-commits mailing list