Skip to content

Commit ef5e523

Browse files
committed
Clang/Preprocessor: Not add headers of __has_include into DepColloctor
When we preprocess the bellow code with clang -E -MD -MF #if __has_include(<limits.h>) // DO NOTHING #endif #if 0 && __has_include(<limits.h>) #include <limits.h> #endif It will list limits.h in the dependencies. The same case with #__has_include_next
1 parent 104ad92 commit ef5e523

File tree

8 files changed

+52
-25
lines changed

8 files changed

+52
-25
lines changed

clang/include/clang/Lex/PPCallbacks.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ class PPCallbacks {
370370
/// read.
371371
virtual void HasInclude(SourceLocation Loc, StringRef FileName, bool IsAngled,
372372
OptionalFileEntryRef File,
373-
SrcMgr::CharacteristicKind FileType);
373+
SrcMgr::CharacteristicKind FileType,
374+
bool AddToDepCollector = true);
374375

375376
/// Hook called when a source range is skipped.
376377
/// \param Range The SourceRange that was skipped. The range begins at the
@@ -621,7 +622,8 @@ class PPChainedCallbacks : public PPCallbacks {
621622

622623
void HasInclude(SourceLocation Loc, StringRef FileName, bool IsAngled,
623624
OptionalFileEntryRef File,
624-
SrcMgr::CharacteristicKind FileType) override;
625+
SrcMgr::CharacteristicKind FileType,
626+
bool AddToDepCollector = true) override;
625627

626628
void PragmaOpenCLExtension(SourceLocation NameLoc, const IdentifierInfo *Name,
627629
SourceLocation StateLoc, unsigned State) override {

clang/lib/Frontend/DependencyFile.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,17 @@ struct DepCollectorPPCallbacks : public PPCallbacks {
104104

105105
void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled,
106106
OptionalFileEntryRef File,
107-
SrcMgr::CharacteristicKind FileType) override {
107+
SrcMgr::CharacteristicKind FileType,
108+
bool AddToDepCollector = true) override {
108109
if (!File)
109110
return;
110111
StringRef Filename =
111112
llvm::sys::path::remove_leading_dotslash(File->getName());
112-
DepCollector.maybeAddDependency(Filename, /*FromModule=*/false,
113-
/*IsSystem=*/isSystem(FileType),
114-
/*IsModuleFile=*/false,
115-
/*IsMissing=*/false);
113+
if (AddToDepCollector)
114+
DepCollector.maybeAddDependency(Filename, /*FromModule=*/false,
115+
/*IsSystem=*/isSystem(FileType),
116+
/*IsModuleFile=*/false,
117+
/*IsMissing=*/false);
116118
}
117119

118120
void EndOfMainFile() override {

clang/lib/Lex/PPCallbacks.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ PPCallbacks::~PPCallbacks() = default;
1515

1616
void PPCallbacks::HasInclude(SourceLocation Loc, StringRef FileName,
1717
bool IsAngled, OptionalFileEntryRef File,
18-
SrcMgr::CharacteristicKind FileType) {}
18+
SrcMgr::CharacteristicKind FileType,
19+
bool AddToDepCollector) {}
1920

2021
// Out of line key method.
2122
PPChainedCallbacks::~PPChainedCallbacks() = default;
2223

2324
void PPChainedCallbacks::HasInclude(SourceLocation Loc, StringRef FileName,
2425
bool IsAngled, OptionalFileEntryRef File,
25-
SrcMgr::CharacteristicKind FileType) {
26-
First->HasInclude(Loc, FileName, IsAngled, File, FileType);
27-
Second->HasInclude(Loc, FileName, IsAngled, File, FileType);
26+
SrcMgr::CharacteristicKind FileType,
27+
bool AddToDepCollector) {
28+
First->HasInclude(Loc, FileName, IsAngled, File, FileType, AddToDepCollector);
29+
Second->HasInclude(Loc, FileName, IsAngled, File, FileType,
30+
AddToDepCollector);
2831
}

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,8 @@ static bool EvaluateHasIncludeCommon(Token &Tok, IdentifierInfo *II,
12561256
SrcMgr::CharacteristicKind FileType = SrcMgr::C_User;
12571257
if (File)
12581258
FileType = PP.getHeaderSearchInfo().getFileDirFlavor(*File);
1259-
Callbacks->HasInclude(FilenameLoc, Filename, isAngled, File, FileType);
1259+
Callbacks->HasInclude(FilenameLoc, Filename, isAngled, File, FileType,
1260+
false);
12601261
}
12611262

12621263
// Get the result value. A result of true means the file exists.

clang/test/ClangScanDeps/has_include_if_elif.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
// RUN: clang-scan-deps -compilation-database %t/cdb.json -mode preprocess | FileCheck %s
3434

3535
// CHECK: tu.c
36-
// CHECK-NEXT: header1.h
37-
// CHECK-NEXT: header2.h
38-
// CHECK-NEXT: header3.h
39-
// CHECK-NEXT: header4.h
36+
// CHECK-NOT: header1.h
37+
// CHECK-NOT: header2.h
38+
// CHECK-NOT: header3.h
39+
// CHECK-NOT: header4.h

clang/test/Frontend/dependency-gen-has-include.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
// RUN: FileCheck -input-file=%t.dir/file.deps %s
1818
// CHECK: dependency-gen-has-include.o
1919
// CHECK: dependency-gen-has-include.c
20-
// CHECK: a{{[/\\]}}header.h
20+
// CHECK-NOT: a{{[/\\]}}header.h
2121
// CHECK-NOT: missing{{[/\\]}}file.h
22-
// CHECK: system{{[/\\]}}system-header.h
22+
// CHECK-NOT: system{{[/\\]}}system-header.h
2323
// CHECK: next-a{{[/\\]}}next-header.h
24-
// CHECK: next-b{{[/\\]}}next-header.h
24+
// CHECK-NOT: next-b{{[/\\]}}next-header.h
2525

2626
// Verify that we ignore system headers in user-only headers mode.
2727
// RUN: %clang -MMD -MF %t.dir/user-headers.deps %s -fsyntax-only -I %t.dir -isystem %t.dir/system -I %t.dir/next-a -I %t.dir/next-b
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Test -MF and -E flags with has_include
2+
3+
#ifdef TEST_HAS_INCLUDE_NEXT
4+
#if __has_include_next(<limits.h>)
5+
// DO NOTHING
6+
#endif
7+
#endif
8+
9+
#ifdef TEST_HAS_INCLUDE
10+
#if __has_include(<limits.h>)
11+
// DO NOTHING
12+
#endif
13+
#endif
14+
15+
// RUN: %clang -DTEST_HAS_INCLUDE -E -MD -MF - %s \
16+
// RUN: | FileCheck -check-prefix=TEST-HAS %s
17+
// TEST-HAS: dependencies-on-has-include.o:
18+
// TEST-HAS-NOT: limits.h
19+
20+
// RUN: %clang -Wno-include-next-outside-header -DTEST_HAS_INCLUDE_NEXT -E -MD -MF - %s \
21+
// RUN: | FileCheck -check-prefix=TEST-HAS-N %s
22+
// TEST-HAS-N: dependencies-on-has-include.o:
23+
// TEST-HAS-N-NOT: limits.h

clang/unittests/Tooling/DependencyScanning/DependencyScannerTest.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,9 @@ TEST(DependencyScanner, ScanDepsReuseFilemanagerHasInclude) {
198198
TestDependencyScanningAction Action(Deps);
199199
Tool.run(&Action);
200200
using llvm::sys::path::convert_to_slash;
201-
ASSERT_EQ(Deps.size(), 6u);
201+
ASSERT_EQ(Deps.size(), 2u);
202202
EXPECT_EQ(convert_to_slash(Deps[0]), "/root/test.cpp");
203-
EXPECT_EQ(convert_to_slash(Deps[1]), "/root/header.h");
204-
EXPECT_EQ(convert_to_slash(Deps[2]), "/root/symlink.h");
205-
EXPECT_EQ(convert_to_slash(Deps[3]), "/root/test.cpp");
206-
EXPECT_EQ(convert_to_slash(Deps[4]), "/root/header.h");
207-
EXPECT_EQ(convert_to_slash(Deps[5]), "/root/symlink.h");
203+
EXPECT_EQ(convert_to_slash(Deps[1]), "/root/test.cpp");
208204
}
209205

210206
TEST(DependencyScanner, ScanDepsWithFS) {

0 commit comments

Comments
 (0)