]> granicus.if.org Git - clang/commitdiff
The macros defined by the language standard are still available even when the
authorNick Lewycky <nicholas@mxc.ca>
Tue, 7 Jun 2011 06:07:12 +0000 (06:07 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Tue, 7 Jun 2011 06:07:12 +0000 (06:07 +0000)
-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

lib/Frontend/InitPreprocessor.cpp
test/Frontend/undef.c [new file with mode: 0644]

index e63036a421ff30c1880cf4b08bc95b4fec415586..26230d6a57b77dcd93ce06d5597858dc9ad4198f 100644 (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)
diff --git a/test/Frontend/undef.c b/test/Frontend/undef.c
new file mode 100644 (file)
index 0000000..f539cdc
--- /dev/null
@@ -0,0 +1,4 @@
+// RUN: %clang -undef -x assembler-with-cpp -E %s
+#ifndef __ASSEMBLER__
+#error "Must be preprocessed as assembler."
+#endif