The macros defined by the language standard are still available even when the

-undef flag is passed in. Also __ASSEMBLER__ with -x assembler-with-cpp. (Don't
ask.)


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@132708 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Nick Lewycky 2011-06-07 06:07:12 +00:00
parent 5b01b83190
commit a9c6441c73
2 changed files with 41 additions and 26 deletions

View File

@ -221,6 +221,37 @@ static void DefineExactWidthIntType(TargetInfo::IntType Ty,
ConstSuffix);
}
static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
const LangOptions &LangOpts,
const FrontendOptions &FEOpts,
MacroBuilder &Builder) {
if (!LangOpts.Microsoft && !LangOpts.TraditionalCPP)
Builder.defineMacro("__STDC__");
if (LangOpts.Freestanding)
Builder.defineMacro("__STDC_HOSTED__", "0");
else
Builder.defineMacro("__STDC_HOSTED__");
if (!LangOpts.CPlusPlus) {
if (LangOpts.C99)
Builder.defineMacro("__STDC_VERSION__", "199901L");
else if (!LangOpts.GNUMode && LangOpts.Digraphs)
Builder.defineMacro("__STDC_VERSION__", "199409L");
} else {
if (LangOpts.GNUMode)
Builder.defineMacro("__cplusplus");
else
// C++ [cpp.predefined]p1:
// The name_ _cplusplus is defined to the value 199711L when compiling a
// C++ translation unit.
Builder.defineMacro("__cplusplus", "199711L");
}
// Not "standard" per se, but available even with the -undef flag.
if (LangOpts.AsmPreprocessor)
Builder.defineMacro("__ASSEMBLER__");
}
static void InitializePredefinedMacros(const TargetInfo &TI,
const LangOptions &LangOpts,
const FrontendOptions &FEOpts,
@ -256,20 +287,6 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
// Initialize language-specific preprocessor defines.
// These should all be defined in the preprocessor according to the
// current language configuration.
if (!LangOpts.Microsoft && !LangOpts.TraditionalCPP)
Builder.defineMacro("__STDC__");
if (LangOpts.AsmPreprocessor)
Builder.defineMacro("__ASSEMBLER__");
if (!LangOpts.CPlusPlus) {
if (LangOpts.C99)
Builder.defineMacro("__STDC_VERSION__", "199901L");
else if (!LangOpts.GNUMode && LangOpts.Digraphs)
Builder.defineMacro("__STDC_VERSION__", "199409L");
}
// Standard conforming mode?
if (!LangOpts.GNUMode)
Builder.defineMacro("__STRICT_ANSI__");
@ -277,11 +294,6 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
if (LangOpts.CPlusPlus0x)
Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");
if (LangOpts.Freestanding)
Builder.defineMacro("__STDC_HOSTED__", "0");
else
Builder.defineMacro("__STDC_HOSTED__");
if (LangOpts.ObjC1) {
Builder.defineMacro("__OBJC__");
if (LangOpts.ObjCNonFragileABI) {
@ -324,13 +336,6 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
if (LangOpts.CPlusPlus) {
Builder.defineMacro("__GNUG__", "4");
Builder.defineMacro("__GXX_WEAK__");
if (LangOpts.GNUMode)
Builder.defineMacro("__cplusplus");
else
// C++ [cpp.predefined]p1:
// The name_ _cplusplusis defined to the value 199711L when compiling a
// C++ translation unit.
Builder.defineMacro("__cplusplus", "199711L");
Builder.defineMacro("__private_extern__", "extern");
}
@ -572,6 +577,12 @@ void clang::InitializePreprocessor(Preprocessor &PP,
InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(),
FEOpts, Builder);
// Even with predefines off, some macros are still predefined.
// These should all be defined in the preprocessor according to the
// current language configuration.
InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(),
FEOpts, Builder);
// Add on the predefines from the driver. Wrap in a #line directive to report
// that they come from the command line.
if (!PP.getLangOptions().AsmPreprocessor)

4
test/Frontend/undef.c Normal file
View File

@ -0,0 +1,4 @@
// RUN: %clang -undef -x assembler-with-cpp -E %s
#ifndef __ASSEMBLER__
#error "Must be preprocessed as assembler."
#endif