Author: Argyrios Kyrtzidis
Date: 2022-05-27T23:59:30-07:00
New Revision: fad6e37995b461a7750bdc203aad37eca9532fd5

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

LOG: [Lex] Fix crash during dependency scanning while skipping an unmatched 
`#if`

Added: 
    clang/test/ClangScanDeps/skipping-unmatched-if.c

Modified: 
    clang/lib/Lex/Lexer.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index a0a7a6ae789b..d4601261b58b 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -4223,6 +4223,7 @@ bool 
Lexer::LexDependencyDirectiveTokenWhileSkipping(Token &Result) {
       }
       break;
     case pp_eof:
+      NextDepDirectiveTokenIndex = 0;
       return LexEndOfFile(Result, BufferEnd);
     }
   } while (!Stop);

diff  --git a/clang/test/ClangScanDeps/skipping-unmatched-if.c 
b/clang/test/ClangScanDeps/skipping-unmatched-if.c
new file mode 100644
index 000000000000..fec7857d6bfd
--- /dev/null
+++ b/clang/test/ClangScanDeps/skipping-unmatched-if.c
@@ -0,0 +1,27 @@
+// Check dependency scanning when skipping an unmatched #if
+
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
+
+// RUN: not clang-scan-deps -compilation-database %t/cdb.json 2>&1 | FileCheck 
%s
+// CHECK: header1.h:1:2: error: unterminated conditional directive
+
+//--- cdb.json.template
+[{
+  "directory" : "DIR",
+  "command" : "clang -target x86_64-apple-macosx10.7 -c DIR/test.cpp -o 
DIR/test.o",
+  "file" : "DIR/test.o"
+}]
+
+//--- test.cpp
+#include "header1.h"
+#include "header2.h"
+
+//--- header1.h
+#if 0
+
+//--- header2.h
+#ifndef _HEADER2_H_
+#define _HEADER2_H_
+#endif


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to