[clang] c68b8c8 - [Lex] Make sure to notify `MultipleIncludeOpt` for "read tokens" during fast dependency directive lexing

Argyrios Kyrtzidis via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 29 15:50:58 PDT 2022


Author: Argyrios Kyrtzidis
Date: 2022-06-29T15:50:16-07:00
New Revision: c68b8c84eb17e4c125897a8a381aa31eea5e5c58

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

LOG: [Lex] Make sure to notify `MultipleIncludeOpt` for "read tokens" during fast dependency directive lexing

Otherwise a header may be erroneously marked as having a header macro guard and won't get re-included.

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

Added: 
    clang/test/ClangScanDeps/more-content-after-headerguard.c

Modified: 
    clang/lib/Lex/Lexer.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index 122861c4d2520..6820057642bea 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -4248,6 +4248,10 @@ bool Lexer::LexDependencyDirectiveToken(Token &Result) {
 
   const dependency_directives_scan::Token &DDTok =
       DepDirectives.front().Tokens[NextDepDirectiveTokenIndex++];
+  if (NextDepDirectiveTokenIndex > 1 || DDTok.Kind != tok::hash) {
+    // Read something other than a preprocessor directive hash.
+    MIOpt.ReadToken();
+  }
 
   const char *TokPtr = convertDependencyDirectiveToken(DDTok, Result);
 

diff  --git a/clang/test/ClangScanDeps/more-content-after-headerguard.c b/clang/test/ClangScanDeps/more-content-after-headerguard.c
new file mode 100644
index 0000000000000..5c47508e03ab3
--- /dev/null
+++ b/clang/test/ClangScanDeps/more-content-after-headerguard.c
@@ -0,0 +1,47 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
+
+// RUN: clang-scan-deps -compilation-database %t/cdb.json | FileCheck %s
+
+// CHECK: t.c
+// CHECK: top.h
+// CHECK: n1.h
+// CHECK: n2.h
+// CHECK: n3.h
+
+//--- cdb.json.template
+[
+  {
+    "directory": "DIR",
+    "command": "clang -fsyntax-only DIR/t.c",
+    "file": "DIR/t.c"
+  }
+]
+
+//--- t.c
+
+#include "top.h"
+#define INCLUDE_N3
+#include "top.h"
+
+//--- top.h
+#ifndef _TOP_H_
+#define _TOP_H_
+
+#include "n1.h"
+
+#endif
+
+// More stuff after following '#endif', should invalidate the macro guard optimization,
+// and allow `top.h` to get re-included.
+#include "n2.h"
+
+//--- n1.h
+
+//--- n2.h
+#ifdef INCLUDE_N3
+#include "n3.h"
+#endif
+
+//--- n3.h


        


More information about the cfe-commits mailing list