Skip to content

Commit d286626

Browse files
authored
Merge pull request #6049 from akyrtzi/pr/stable2/has-include-lex-fix
[stable/20221013][Lex] For dependency directive lexing, angled includes in `__has_include` should be lexed as string literals
2 parents 8c3c3fb + 1b8a90d commit d286626

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

clang/lib/Lex/Lexer.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4367,6 +4367,22 @@ bool Lexer::LexDependencyDirectiveToken(Token &Result) {
43674367
MIOpt.ReadToken();
43684368
}
43694369

4370+
if (ParsingFilename && DDTok.is(tok::less)) {
4371+
BufferPtr = BufferStart + DDTok.Offset;
4372+
LexAngledStringLiteral(Result, BufferPtr + 1);
4373+
if (Result.isNot(tok::header_name))
4374+
return true;
4375+
// Advance the index of lexed tokens.
4376+
while (true) {
4377+
const dependency_directives_scan::Token &NextTok =
4378+
DepDirectives.front().Tokens[NextDepDirectiveTokenIndex];
4379+
if (BufferStart + NextTok.Offset >= BufferPtr)
4380+
break;
4381+
++NextDepDirectiveTokenIndex;
4382+
}
4383+
return true;
4384+
}
4385+
43704386
const char *TokPtr = convertDependencyDirectiveToken(DDTok, Result);
43714387

43724388
if (Result.is(tok::hash) && Result.isAtStartOfLine()) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
4+
5+
// RUN: clang-scan-deps -compilation-database %t/cdb.json | FileCheck %s
6+
// CHECK: t.c
7+
// CHECK: something.h
8+
9+
// RUN: sed -e "s|DIR|%/t|g" %t/cdb-error.json.template > %t/cdb-error.json
10+
// RUN: not clang-scan-deps -compilation-database %t/cdb-error.json 2>&1 | FileCheck %s -check-prefix=ERROR
11+
// ERROR: error: expected '>'
12+
// ERROR: error: expected value in expression
13+
14+
//--- cdb.json.template
15+
[
16+
{
17+
"directory": "DIR",
18+
"command": "clang -fsyntax-only DIR/t.c -I DIR",
19+
"file": "DIR/t.c"
20+
}
21+
]
22+
23+
//--- cdb-error.json.template
24+
[
25+
{
26+
"directory": "DIR",
27+
"command": "clang -fsyntax-only DIR/error.c",
28+
"file": "DIR/error.c"
29+
}
30+
]
31+
32+
//--- t.c
33+
34+
#define something
35+
36+
// Make sure the include is lexed as a literal, ignoring the macro.
37+
#if __has_include(<something/something.h>)
38+
#include <something/something.h>
39+
#endif
40+
41+
//--- something/something.h
42+
43+
//--- error.c
44+
#if __has_include(<something/something.h)
45+
#define MAC
46+
#endif

0 commit comments

Comments
 (0)