Skip to content

Commit 5c086c7

Browse files
author
Erich Keane
committed
For Linux/gnu compatibility, preinclude <stdc-predef.h> if the file is available
As reported in llvm bugzilla 32377. Here’s a patch to add preinclude of stdc-predef.h. The gcc documentation says “On GNU/Linux, <stdc-predef.h> is pre-included.” See https://gcc.gnu.org/gcc-4.8/porting_to.html; The preinclude is inhibited with –ffreestanding. Basically I fixed the failing test cases by adding –ffreestanding which inhibits this behavior. I fixed all the failing tests, including some in extra/test, there's a separate patch for that which is linked here Patch By: mibintc Differential Revision: https://reviews.llvm.org/D34158 llvm-svn: 318669
1 parent 991447d commit 5c086c7

20 files changed

+99
-56
lines changed

clang/include/clang/Driver/CC1Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,8 @@ def token_cache : Separate<["-"], "token-cache">, MetaVarName<"<path>">,
769769
HelpText<"Use specified token cache file">;
770770
def detailed_preprocessing_record : Flag<["-"], "detailed-preprocessing-record">,
771771
HelpText<"include a detailed record of preprocessing actions">;
772+
def fsystem_include_if_exists : Separate<["-"], "fsystem-include-if-exists">, MetaVarName<"<file>">,
773+
HelpText<"Include system file before parsing if file exists">;
772774

773775
//===----------------------------------------------------------------------===//
774776
// OpenCL Options

clang/include/clang/Lex/PreprocessorOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class PreprocessorOptions {
6060
/// \brief Headers that will be converted to chained PCHs in memory.
6161
std::vector<std::string> ChainedIncludes;
6262

63+
/// \brief System Headers that are pre-included if they exist.
64+
std::vector<std::string> FSystemIncludeIfExists;
65+
6366
/// \brief When true, disables most of the normal validation performed on
6467
/// precompiled headers.
6568
bool DisablePCHValidation;
@@ -190,6 +193,7 @@ class PreprocessorOptions {
190193
DumpDeserializedPCHDecls = false;
191194
ImplicitPCHInclude.clear();
192195
ImplicitPTHInclude.clear();
196+
FSystemIncludeIfExists.clear();
193197
TokenCache.clear();
194198
SingleFileParseMode = false;
195199
LexEditorPlaceholders = true;

clang/lib/Driver/Job.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static bool skipArgs(const char *Flag, bool HaveCrashVFS, int &SkipNum,
6464
.Cases("-internal-externc-isystem", "-iprefix", true)
6565
.Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
6666
.Cases("-isysroot", "-I", "-F", "-resource-dir", true)
67-
.Cases("-iframework", "-include-pch", true)
67+
.Cases("-iframework", "-include-pch", "-fsystem-include-if-exists", true)
6868
.Default(false);
6969
if (IsInclude)
7070
return HaveCrashVFS ? false : true;

clang/lib/Driver/ToolChains/Linux.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,8 @@ void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
710710
addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
711711

712712
addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
713+
714+
AddGnuIncludeArgs(DriverArgs, CC1Args);
713715
}
714716

715717
static std::string DetectLibcxxIncludePath(StringRef base) {
@@ -748,6 +750,17 @@ std::string Linux::findLibCxxIncludePath() const {
748750
return "";
749751
}
750752

753+
void Linux::AddGnuIncludeArgs(const llvm::opt::ArgList &DriverArgs,
754+
llvm::opt::ArgStringList &CC1Args) const {
755+
if (!DriverArgs.hasArg(options::OPT_ffreestanding)) {
756+
// For gcc compatibility, clang will preinclude <stdc-predef.h>
757+
// -ffreestanding suppresses this behavior.
758+
CC1Args.push_back("-fsystem-include-if-exists");
759+
CC1Args.push_back("stdc-predef.h");
760+
}
761+
}
762+
763+
751764
void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
752765
llvm::opt::ArgStringList &CC1Args) const {
753766
// We need a detected GCC installation on Linux to provide libstdc++'s

clang/lib/Driver/ToolChains/Linux.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF {
3131
void addLibStdCxxIncludePaths(
3232
const llvm::opt::ArgList &DriverArgs,
3333
llvm::opt::ArgStringList &CC1Args) const override;
34+
void AddGnuIncludeArgs(const llvm::opt::ArgList &DriverArgs,
35+
llvm::opt::ArgStringList &CC1Args) const;
3436
void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
3537
llvm::opt::ArgStringList &CC1Args) const override;
3638
void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs,

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,6 +2594,10 @@ static void ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args,
25942594
for (const Arg *A : Args.filtered(OPT_chain_include))
25952595
Opts.ChainedIncludes.emplace_back(A->getValue());
25962596

2597+
// Add the ordered list of -fsystem-include-if-exists.
2598+
for (const Arg *A : Args.filtered(OPT_fsystem_include_if_exists))
2599+
Opts.FSystemIncludeIfExists.emplace_back(A->getValue());
2600+
25972601
for (const Arg *A : Args.filtered(OPT_remap_file)) {
25982602
std::pair<StringRef, StringRef> Split = StringRef(A->getValue()).split(';');
25992603

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ static void AddImplicitInclude(MacroBuilder &Builder, StringRef File) {
7070
Builder.append(Twine("#include \"") + File + "\"");
7171
}
7272

73+
/// AddImplicitSystemIncludeIfExists - Add an implicit system \#include of the
74+
/// specified file to the predefines buffer: precheck with __has_include.
75+
static void AddImplicitSystemIncludeIfExists(MacroBuilder &Builder,
76+
StringRef File) {
77+
Builder.append(Twine("#if __has_include( <") + File + ">)");
78+
Builder.append(Twine("#include <") + File + ">");
79+
Builder.append(Twine("#endif"));
80+
}
81+
7382
static void AddImplicitIncludeMacros(MacroBuilder &Builder, StringRef File) {
7483
Builder.append(Twine("#__include_macros \"") + File + "\"");
7584
// Marker token to stop the __include_macros fetch loop.
@@ -1110,6 +1119,13 @@ void clang::InitializePreprocessor(
11101119
// Exit the command line and go back to <built-in> (2 is LC_LEAVE).
11111120
if (!PP.getLangOpts().AsmPreprocessor)
11121121
Builder.append("# 1 \"<built-in>\" 2");
1122+
1123+
// Process -fsystem-include-if-exists directives.
1124+
for (unsigned i = 0,
1125+
e = InitOpts.FSystemIncludeIfExists.size(); i != e; ++i) {
1126+
const std::string &Path = InitOpts.FSystemIncludeIfExists[i];
1127+
AddImplicitSystemIncludeIfExists(Builder, Path);
1128+
}
11131129

11141130
// If -imacros are specified, include them now. These are processed before
11151131
// any -include directives.

clang/test/Driver/crash-report-header.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: rm -rf %t
22
// RUN: mkdir %t
3-
// RUN: not env TMPDIR="%t" TEMP="%t" TMP="%t" RC_DEBUG_OPTIONS=1 %clang -fsyntax-only %s 2>&1 | FileCheck %s
3+
// RUN: not env TMPDIR="%t" TEMP="%t" TMP="%t" RC_DEBUG_OPTIONS=1 %clang -ffreestanding -fsyntax-only %s 2>&1 | FileCheck %s
44
// RUN: cat %t/crash-report-header-*.h | FileCheck --check-prefix=CHECKSRC "%s"
55
// RUN: cat %t/crash-report-header-*.sh | FileCheck --check-prefix=CHECKSH "%s"
66
// REQUIRES: crash-recovery

clang/test/Driver/crash-report-spaces.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: rm -rf "%t"
22
// RUN: mkdir "%t"
33
// RUN: cp "%s" "%t/crash report spaces.c"
4-
// RUN: not env TMPDIR="%t" TEMP="%t" TMP="%t" RC_DEBUG_OPTIONS=1 %clang -fsyntax-only "%t/crash report spaces.c" 2>&1 | FileCheck "%s"
4+
// RUN: not env TMPDIR="%t" TEMP="%t" TMP="%t" RC_DEBUG_OPTIONS=1 %clang -ffreestanding -fsyntax-only "%t/crash report spaces.c" 2>&1 | FileCheck "%s"
55
// RUN: cat "%t/crash report spaces"-*.c | FileCheck --check-prefix=CHECKSRC "%s"
66
// RUN: cat "%t/crash report spaces"-*.sh | FileCheck --check-prefix=CHECKSH "%s"
77
// REQUIRES: crash-recovery

clang/test/Driver/crash-report.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RUN: mkdir %t
33
// RUN: not env TMPDIR=%t TEMP=%t TMP=%t RC_DEBUG_OPTIONS=1 \
44
// RUN: CC_PRINT_HEADERS=1 CC_LOG_DIAGNOSTICS=1 \
5-
// RUN: %clang -fsyntax-only %s \
5+
// RUN: %clang -fsyntax-only -ffreestanding %s \
66
// RUN: -F/tmp/ -I /tmp/ -idirafter /tmp/ -iquote /tmp/ -isystem /tmp/ \
77
// RUN: -iprefix /the/prefix -iwithprefix /tmp -iwithprefixbefore /tmp/ \
88
// RUN: -Xclang -internal-isystem -Xclang /tmp/ \
@@ -42,5 +42,6 @@ FOO
4242
// CHECKSH-NOT: "-iwithprefixbefore" "/tmp/"
4343
// CHECKSH-NOT: "-internal-isystem" "/tmp/"
4444
// CHECKSH-NOT: "-internal-externc-isystem" "/tmp/"
45+
// CHECKSH-NOT: "-fsystem-include-if-exists" "/tmp/"
4546
// CHECKSH-NOT: "-dwarf-debug-flags"
4647
// CHECKSH: "crash-report-{{[^ ]*}}.c"

clang/test/Driver/rewrite-map-in-diagnostics.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: rm -rf "%t"
22
// RUN: mkdir -p "%t"
33
// RUN: not env TMPDIR="%t" TEMP="%t" TMP="%t" RC_DEBUG_OPTION=1 \
4-
// RUN: %clang -fsyntax-only -frewrite-map-file %p/Inputs/rewrite.map %s 2>&1 \
4+
// RUN: %clang -ffreestanding -fsyntax-only -frewrite-map-file %p/Inputs/rewrite.map %s 2>&1 \
55
// RUN: | FileCheck %s
66

77
#pragma clang __debug parser_crash

clang/test/Index/IBOutletCollection.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ @interface Test {
55
}
66
@end
77

8-
// RUN: c-index-test -cursor-at=%s:4:24 %s | FileCheck -check-prefix=CHECK-CURSOR %s
8+
// RUN: c-index-test -cursor-at=%s:4:24 -ffreestanding %s | FileCheck -check-prefix=CHECK-CURSOR %s
99
// CHECK-CURSOR: ObjCClassRef=Test:3:12
1010

11-
// RUN: c-index-test -test-annotate-tokens=%s:4:1:5:1 %s | FileCheck -check-prefix=CHECK-TOK %s
11+
// RUN: c-index-test -test-annotate-tokens=%s:4:1:5:1 -ffreestanding %s | FileCheck -check-prefix=CHECK-TOK %s
1212
// CHECK-TOK: Identifier: "IBOutletCollection" [4:3 - 4:21] macro expansion=IBOutletCollection:1:9
1313
// FIXME: The following token should belong to the macro expansion cursor.
1414
// CHECK-TOK: Punctuation: "(" [4:21 - 4:22] attribute(iboutletcollection)= [IBOutletCollection=ObjCInterface]

clang/test/Index/annotate-macro-args.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Test without PCH
2-
// RUN: c-index-test -test-annotate-tokens=%S/annotate-macro-args.h:9:1:10:1 %s -include %S/annotate-macro-args.h | FileCheck -check-prefix=CHECK1 %s
3-
// RUN: c-index-test -test-annotate-tokens=%S/annotate-macro-args.h:15:1:16:1 %s -include %S/annotate-macro-args.h | FileCheck -check-prefix=CHECK2 %s
2+
// RUN: c-index-test -test-annotate-tokens=%S/annotate-macro-args.h:9:1:10:1 %s -ffreestanding -include %S/annotate-macro-args.h | FileCheck -check-prefix=CHECK1 %s
3+
// RUN: c-index-test -test-annotate-tokens=%S/annotate-macro-args.h:15:1:16:1 %s -ffreestanding -include %S/annotate-macro-args.h | FileCheck -check-prefix=CHECK2 %s
44

55
// Test with PCH
6-
// RUN: c-index-test -write-pch %t.pch -x objective-c-header %S/annotate-macro-args.h -Xclang -detailed-preprocessing-record
7-
// RUN: c-index-test -test-annotate-tokens=%S/annotate-macro-args.h:9:1:10:1 %s -include-pch %t.pch | FileCheck -check-prefix=CHECK1 %s
8-
// RUN: c-index-test -test-annotate-tokens=%S/annotate-macro-args.h:15:1:16:1 %s -include-pch %t.pch | FileCheck -check-prefix=CHECK2 %s
6+
// RUN: c-index-test -write-pch %t.pch -x objective-c-header %S/annotate-macro-args.h -ffreestanding -Xclang -detailed-preprocessing-record
7+
// RUN: c-index-test -test-annotate-tokens=%S/annotate-macro-args.h:9:1:10:1 %s -ffreestanding -include-pch %t.pch | FileCheck -check-prefix=CHECK1 %s
8+
// RUN: c-index-test -test-annotate-tokens=%S/annotate-macro-args.h:15:1:16:1 %s -ffreestanding -include-pch %t.pch | FileCheck -check-prefix=CHECK2 %s
99

1010
// CHECK1: Identifier: "MACRO" [9:3 - 9:8] macro expansion=MACRO:6:9
1111
// CHECK1: Punctuation: "(" [9:8 - 9:9]

clang/test/Index/annotate-tokens-pp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ struct A
4242
#endif
4343
};
4444

45-
// RUN: c-index-test -test-annotate-tokens=%s:2:1:44:1 -I%S/Inputs %s | FileCheck %s
46-
// RUN: env CINDEXTEST_EDITING=1 c-index-test -test-annotate-tokens=%s:2:1:44:1 -I%S/Inputs %s | FileCheck %s
45+
// RUN: c-index-test -test-annotate-tokens=%s:2:1:44:1 -I%S/Inputs -ffreestanding %s | FileCheck %s
46+
// RUN: env CINDEXTEST_EDITING=1 c-index-test -test-annotate-tokens=%s:2:1:44:1 -I%S/Inputs -ffreestanding %s | FileCheck %s
4747
// CHECK: Punctuation: "#" [2:1 - 2:2] preprocessing directive=
4848
// CHECK: Identifier: "define" [2:2 - 2:8] preprocessing directive=
4949
// CHECK: Identifier: "STILL_NOTHING" [2:9 - 2:22] macro definition=STILL_NOTHING

clang/test/Index/annotate-tokens.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void test() {
6868
reg.field = 1;
6969
}
7070

71-
// RUN: c-index-test -test-annotate-tokens=%s:4:1:37:1 %s | FileCheck %s
71+
// RUN: c-index-test -test-annotate-tokens=%s:4:1:37:1 -ffreestanding %s | FileCheck %s
7272
// CHECK: Identifier: "T" [4:3 - 4:4] TypeRef=T:1:13
7373
// CHECK: Punctuation: "*" [4:4 - 4:5] VarDecl=t_ptr:4:6 (Definition)
7474
// CHECK: Identifier: "t_ptr" [4:6 - 4:11] VarDecl=t_ptr:4:6 (Definition)
@@ -191,10 +191,10 @@ void test() {
191191
// CHECK: Punctuation: ")" [36:97 - 36:98] FunctionDecl=test:36:63 (unavailable) (always unavailable: "")
192192
// CHECK: Punctuation: ";" [36:98 - 36:99]
193193

194-
// RUN: c-index-test -test-annotate-tokens=%s:4:1:165:32 %s | FileCheck %s
195-
// RUN: c-index-test -test-annotate-tokens=%s:4:1:165:38 %s | FileCheck %s
194+
// RUN: c-index-test -test-annotate-tokens=%s:4:1:165:32 -ffreestanding %s | FileCheck %s
195+
// RUN: c-index-test -test-annotate-tokens=%s:4:1:165:38 -ffreestanding %s | FileCheck %s
196196

197-
// RUN: c-index-test -test-annotate-tokens=%s:50:1:55:1 %s | FileCheck %s -check-prefix=CHECK-RANGE1
197+
// RUN: c-index-test -test-annotate-tokens=%s:50:1:55:1 -ffreestanding %s | FileCheck %s -check-prefix=CHECK-RANGE1
198198
// CHECK-RANGE1: Keyword: "void" [50:1 - 50:5] FunctionDecl=func1:50:6
199199
// CHECK-RANGE1: Identifier: "func1" [50:6 - 50:11] FunctionDecl=func1:50:6
200200
// CHECK-RANGE1: Punctuation: "(" [50:11 - 50:12] FunctionDecl=func1:50:6
@@ -216,7 +216,7 @@ void test() {
216216
// CHECK-RANGE1: Literal: "1" [54:10 - 54:11] IntegerLiteral=
217217
// CHECK-RANGE1: Punctuation: "," [54:11 - 54:12] InitListExpr=
218218

219-
// RUN: c-index-test -test-annotate-tokens=%s:54:1:70:1 %s | FileCheck %s -check-prefix=CHECK-RANGE2
219+
// RUN: c-index-test -test-annotate-tokens=%s:54:1:70:1 -ffreestanding %s | FileCheck %s -check-prefix=CHECK-RANGE2
220220
// CHECK-RANGE2: Punctuation: "." [54:5 - 54:6] UnexposedExpr=
221221
// CHECK-RANGE2: Identifier: "y" [54:6 - 54:7] MemberRef=y:52:1
222222
// CHECK-RANGE2: Punctuation: "=" [54:8 - 54:9] UnexposedExpr=
@@ -240,6 +240,6 @@ void test() {
240240
// CHECK-RANGE2: Punctuation: "." [68:6 - 68:7] MemberRefExpr=field:62:9
241241
// CHECK-RANGE2: Identifier: "field" [68:7 - 68:12] MemberRefExpr=field:62:9
242242

243-
// RUN: c-index-test -test-annotate-tokens=%s:68:15:68:16 %s | FileCheck %s -check-prefix=CHECK-RANGE3
243+
// RUN: c-index-test -test-annotate-tokens=%s:68:15:68:16 -ffreestanding %s | FileCheck %s -check-prefix=CHECK-RANGE3
244244
// CHECK-RANGE3: Literal: "1" [68:15 - 68:16] IntegerLiteral=
245245
// CHECK-RANGE3-NOT: Punctuation: ";"

clang/test/Index/c-index-getCursor-test.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: c-index-test -write-pch %t.ast -arch x86_64 -mmacosx-version-min=10.6 -fblocks -x objective-c %s
2-
// RUN: c-index-test -test-file-scan %t.ast %s > %t 2>&1 && FileCheck --input-file=%t %s
1+
// RUN: c-index-test -write-pch %t.ast -arch x86_64 -mmacosx-version-min=10.6 -fblocks -x objective-c %s -ffreestanding
2+
// RUN: c-index-test -test-file-scan %t.ast %s -ffreestanding > %t 2>&1 && FileCheck --input-file=%t %s
33
@interface Foo
44
{
55
}

clang/test/Index/get-cursor-macro-args.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
// RUN: -cursor-at=%S/get-cursor-macro-args.h:9:22 \
66
// RUN: -cursor-at=%S/get-cursor-macro-args.h:15:12 \
77
// RUN: -cursor-at=%S/get-cursor-macro-args.h:15:20 \
8-
// RUN: %s -include %S/get-cursor-macro-args.h | FileCheck %s
8+
// RUN: %s -ffreestanding -include %S/get-cursor-macro-args.h | FileCheck %s
99

1010
// Test with PCH
11-
// RUN: c-index-test -write-pch %t.pch -x objective-c-header %S/get-cursor-macro-args.h
11+
// RUN: c-index-test -write-pch %t.pch -x objective-c-header %S/get-cursor-macro-args.h -ffreestanding
1212
// RUN: c-index-test -cursor-at=%S/get-cursor-macro-args.h:9:12 \
1313
// RUN: -cursor-at=%S/get-cursor-macro-args.h:9:21 \
1414
// RUN: -cursor-at=%S/get-cursor-macro-args.h:9:9 \
1515
// RUN: -cursor-at=%S/get-cursor-macro-args.h:9:22 \
1616
// RUN: -cursor-at=%S/get-cursor-macro-args.h:15:12 \
1717
// RUN: -cursor-at=%S/get-cursor-macro-args.h:15:20 \
18-
// RUN: %s -include-pch %t.pch | FileCheck %s
18+
// RUN: %s -ffreestanding -include-pch %t.pch | FileCheck %s
1919

2020
// CHECK: ObjCClassRef=MyClass:1:12
2121
// CHECK-NEXT: ObjCMessageExpr=meth:2:8

0 commit comments

Comments
 (0)