Skip to content

Commit bcda126

Browse files
committed
clang-cl: Add a /showIncludes:user flag.
This flag is like /showIncludes, but it only includes user headers and omits system headers (similar to MD and MMD). The motivation is that projects that already track system includes though other means can use this flag to get consistent behavior on Windows and non-Windows, and it saves tools that output /showIncludes output (e.g. ninja) some work. implementation-wise, this makes `HeaderIncludesCallback` honor the existing `IncludeSystemHeaders` bit, and changes the three clients of `HeaderIncludesCallback` (`/showIncludes`, `-H`, `CC_PRINT_HEADERS=1`) to pass `-sys-header-deps` to set that bit -- except for `/showIncludes:user`, which doesn't pass it. Differential Revision: https://reviews.llvm.org/D75093
1 parent e551333 commit bcda126

File tree

7 files changed

+64
-19
lines changed

7 files changed

+64
-19
lines changed

clang/include/clang/Driver/CLCompatOptions.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@ def _SLASH_Qvec : CLFlag<"Qvec">,
156156
def _SLASH_Qvec_ : CLFlag<"Qvec-">,
157157
HelpText<"Disable the loop vectorization passes">, Alias<fno_vectorize>;
158158
def _SLASH_showIncludes : CLFlag<"showIncludes">,
159-
HelpText<"Print info about included files to stderr">,
160-
Alias<show_includes>;
159+
HelpText<"Print info about included files to stderr">;
160+
def _SLASH_showIncludes_user : CLFlag<"showIncludes:user">,
161+
HelpText<"Like /showIncludes but omit system headers">;
161162
def _SLASH_showFilenames : CLFlag<"showFilenames">,
162163
HelpText<"Print the name of each compiled file">;
163164
def _SLASH_showFilenames_ : CLFlag<"showFilenames-">,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4758,11 +4758,17 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
47584758
}
47594759

47604760
Args.AddAllArgs(CmdArgs, options::OPT_v);
4761-
Args.AddLastArg(CmdArgs, options::OPT_H);
4761+
4762+
if (Args.getLastArg(options::OPT_H)) {
4763+
CmdArgs.push_back("-H");
4764+
CmdArgs.push_back("-sys-header-deps");
4765+
}
4766+
47624767
if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
47634768
CmdArgs.push_back("-header-include-file");
47644769
CmdArgs.push_back(D.CCPrintHeadersFilename ? D.CCPrintHeadersFilename
47654770
: "-");
4771+
CmdArgs.push_back("-sys-header-deps");
47664772
}
47674773
Args.AddLastArg(CmdArgs, options::OPT_P);
47684774
Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
@@ -6433,7 +6439,13 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType,
64336439
CmdArgs.push_back("--dependent-lib=oldnames");
64346440
}
64356441

6436-
Args.AddLastArg(CmdArgs, options::OPT_show_includes);
6442+
if (Arg *ShowIncludes =
6443+
Args.getLastArg(options::OPT__SLASH_showIncludes,
6444+
options::OPT__SLASH_showIncludes_user)) {
6445+
CmdArgs.push_back("--show-includes");
6446+
if (ShowIncludes->getOption().matches(options::OPT__SLASH_showIncludes))
6447+
CmdArgs.push_back("-sys-header-deps");
6448+
}
64376449

64386450
// This controls whether or not we emit RTTI data for polymorphic types.
64396451
if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR,

clang/lib/Frontend/DependencyFile.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ struct DepCollectorASTListener : public ASTReaderListener {
137137
};
138138
} // end anonymous namespace
139139

140-
void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule,
141-
bool IsSystem, bool IsModuleFile,
142-
bool IsMissing) {
140+
void DependencyCollector::maybeAddDependency(StringRef Filename,
141+
bool FromModule, bool IsSystem,
142+
bool IsModuleFile,
143+
bool IsMissing) {
143144
if (sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
144145
addDependency(Filename);
145146
}
@@ -160,8 +161,8 @@ static bool isSpecialFilename(StringRef Filename) {
160161
}
161162

162163
bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
163-
bool IsSystem, bool IsModuleFile,
164-
bool IsMissing) {
164+
bool IsSystem, bool IsModuleFile,
165+
bool IsMissing) {
165166
return !isSpecialFilename(Filename) &&
166167
(needSystemDependencies() || !IsSystem);
167168
}

clang/lib/Frontend/HeaderIncludeGen.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ void clang::AttachHeaderIncludeGen(Preprocessor &PP,
127127

128128
void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
129129
FileChangeReason Reason,
130-
SrcMgr::CharacteristicKind NewFileType,
131-
FileID PrevFID) {
130+
SrcMgr::CharacteristicKind NewFileType,
131+
FileID PrevFID) {
132132
// Unless we are exiting a #include, make sure to skip ahead to the line the
133133
// #include directive was at.
134134
PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
@@ -167,6 +167,9 @@ void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
167167
else if (!DepOpts.ShowIncludesPretendHeader.empty())
168168
++IncludeDepth; // Pretend inclusion by ShowIncludesPretendHeader.
169169

170+
if (!DepOpts.IncludeSystemHeaders && isSystem(NewFileType))
171+
ShowHeader = false;
172+
170173
// Dump the header include information we are past the predefines buffer or
171174
// are showing all headers and this isn't the magic implicit <command line>
172175
// header.

clang/test/Driver/cl-options.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,16 @@
199199
// RUN: %clang_cl /Qvec /Qvec- -### -- %s 2>&1 | FileCheck -check-prefix=Qvec_ %s
200200
// Qvec_-NOT: -vectorize-loops
201201

202-
// RUN: %clang_cl /showIncludes -### -- %s 2>&1 | FileCheck -check-prefix=showIncludes %s
203-
// showIncludes: --show-includes
202+
// RUN: %clang_cl /showIncludes -### -- %s 2>&1 | FileCheck -check-prefix=showIncludes_ %s
203+
// showIncludes_: --show-includes
204+
// showIncludes_: -sys-header-deps
205+
206+
// RUN: %clang_cl /showIncludes:user -### -- %s 2>&1 | FileCheck -check-prefix=showIncludesUser %s
207+
// showIncludesUser: --show-includes
208+
// showIncludesUser-NOT: -sys-header-deps
204209

205210
// RUN: %clang_cl /E /showIncludes -### -- %s 2>&1 | FileCheck -check-prefix=showIncludes_E %s
211+
// RUN: %clang_cl /E /showIncludes:user -### -- %s 2>&1 | FileCheck -check-prefix=showIncludes_E %s
206212
// RUN: %clang_cl /EP /showIncludes -### -- %s 2>&1 | FileCheck -check-prefix=showIncludes_E %s
207213
// RUN: %clang_cl /E /EP /showIncludes -### -- %s 2>&1 | FileCheck -check-prefix=showIncludes_E %s
208214
// RUN: %clang_cl /EP /P /showIncludes -### -- %s 2>&1 | FileCheck -check-prefix=showIncludes_E %s
Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,51 @@
1-
// RUN: %clang_cc1 -I%S -include Inputs/test3.h -E -H -o /dev/null %s 2> %t.stderr
1+
// RUN: %clang_cc1 -I%S -include Inputs/test3.h -isystem %S/Inputs/SystemHeaderPrefix \
2+
// RUN: -E -H -o /dev/null %s 2> %t.stderr
23
// RUN: FileCheck < %t.stderr %s
34

45
// CHECK-NOT: test3.h
6+
// CHECK-NOT: . {{.*noline.h}}
57
// CHECK: . {{.*test.h}}
68
// CHECK: .. {{.*test2.h}}
79

8-
// RUN: %clang_cc1 -I%S -include Inputs/test3.h --show-includes -o /dev/null %s | \
10+
// RUN: %clang_cc1 -I%S -include Inputs/test3.h -isystem %S/Inputs/SystemHeaderPrefix \
11+
// RUN: -E -H -sys-header-deps -o /dev/null %s 2> %t.stderr
12+
// RUN: FileCheck --check-prefix SYSHEADERS < %t.stderr %s
13+
14+
// SYSHEADERS-NOT: test3.h
15+
// SYSHEADERS: . {{.*noline.h}}
16+
// SYSHEADERS: . {{.*test.h}}
17+
// SYSHEADERS: .. {{.*test2.h}}
18+
19+
// RUN: %clang_cc1 -I%S -include Inputs/test3.h -isystem %S/Inputs/SystemHeaderPrefix \
20+
// RUN: --show-includes -o /dev/null %s | \
921
// RUN: FileCheck --strict-whitespace --check-prefix=MS-STDOUT %s
1022
// MS-STDOUT-NOT: <command line>
23+
// MS-STDOUT-NOT: Note: including file: {{[^ ]*noline.h}}
1124
// MS-STDOUT: Note: including file: {{[^ ]*test3.h}}
1225
// MS-STDOUT: Note: including file: {{[^ ]*test.h}}
1326
// MS-STDOUT: Note: including file: {{[^ ]*test2.h}}
1427
// MS-STDOUT-NOT: Note
1528

16-
// RUN: %clang_cc1 -I%S -include Inputs/test3.h -E --show-includes -o /dev/null %s 2> %t.stderr
29+
// RUN: %clang_cc1 -I%S -include Inputs/test3.h -isystem %S/Inputs/SystemHeaderPrefix \
30+
// RUN: -E --show-includes -o /dev/null %s 2> %t.stderr
1731
// RUN: FileCheck --strict-whitespace --check-prefix=MS-STDERR < %t.stderr %s
1832
// MS-STDERR-NOT: <command line>
33+
// MS-STDERR-NOT: Note: including file: {{[^ ]*noline.h}}
1934
// MS-STDERR: Note: including file: {{[^ ]*test3.h}}
2035
// MS-STDERR: Note: including file: {{[^ ]*test.h}}
2136
// MS-STDERR: Note: including file: {{[^ ]*test2.h}}
2237
// MS-STDERR-NOT: Note
2338

2439
// RUN: echo "fun:foo" > %t.blacklist
25-
// RUN: %clang_cc1 -I%S -fsanitize=address -fdepfile-entry=%t.blacklist --show-includes -o /dev/null %s | \
40+
// RUN: %clang_cc1 -I%S -isystem %S/Inputs/SystemHeaderPrefix \
41+
// RUN: -fsanitize=address -fdepfile-entry=%t.blacklist \
42+
// RUN: --show-includes -o /dev/null %s | \
2643
// RUN: FileCheck --strict-whitespace --check-prefix=MS-BLACKLIST %s
2744
// MS-BLACKLIST: Note: including file: {{[^ ]*\.blacklist}}
45+
// MS-BLACKLIST-NOT: Note: including file: {{[^ ]*noline.h}}
2846
// MS-BLACKLIST: Note: including file: {{[^ ]*test.h}}
2947
// MS-BLACKLIST: Note: including file: {{[^ ]*test2.h}}
3048
// MS-BLACKLIST-NOT: Note
3149

50+
#include <noline.h>
3251
#include "Inputs/test.h"

clang/test/Preprocessor/headermap-rel2.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// RUN: rm -f %t.hmap
22
// RUN: %hmaptool write %S/Inputs/headermap-rel2/project-headers.hmap.json %t.hmap
3-
// RUN: %clang_cc1 -v -fsyntax-only %s -iquote %t.hmap -isystem %S/Inputs/headermap-rel2/system/usr/include -I %S/Inputs/headermap-rel2 -H
4-
// RUN: %clang_cc1 -fsyntax-only %s -iquote %t.hmap -isystem %S/Inputs/headermap-rel2/system/usr/include -I %S/Inputs/headermap-rel2 -H 2> %t.out
3+
4+
// RUN: %clang -fsyntax-only %s -iquote %t.hmap -isystem %S/Inputs/headermap-rel2/system/usr/include -I %S/Inputs/headermap-rel2 -H 2> %t.out
5+
// RUN: FileCheck %s -input-file %t.out
6+
7+
// RUN: env CC_PRINT_HEADERS=1 %clang -fsyntax-only %s -iquote %t.hmap -isystem %S/Inputs/headermap-rel2/system/usr/include -I %S/Inputs/headermap-rel2 2> %t.out
58
// RUN: FileCheck %s -input-file %t.out
69

710
// CHECK: Product/someheader.h

0 commit comments

Comments
 (0)