More fixes to builtin preprocessor defines.
 - Add -static-define option driver can use when __STATIC__ should be
   defined (instead of __DYNAMIC__).

 - Don't set __OPTIMIZE_SIZE__ on Os, __OPTIMIZE_SIZE__ is tied to Oz.

 - Set __NO_INLINE__ following GCC 4.2.

 - Set __GNU_GNU_INLINE__ or __GNU_STDC_INLINE__ following GCC 4.2.

 - Set __EXCEPTIONS for Objective-C NonFragile ABI.

 - Set __STRICT_ANSI__ for standard conforming modes.

 - I added a clang style test case in utils for this, but its not
   particularly portable and I don't think it belongs in the test
   suite.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68621 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/LangOptions.h b/include/clang/Basic/LangOptions.h
index f95024d..5db1d29 100644
--- a/include/clang/Basic/LangOptions.h
+++ b/include/clang/Basic/LangOptions.h
@@ -68,8 +68,14 @@
   unsigned Optimize          : 1; // Whether __OPTIMIZE__ should be defined.
   unsigned OptimizeSize      : 1; // Whether __OPTIMIZE_SIZE__ should be 
                                   // defined.
+  unsigned Static            : 1; // Should __STATIC__ be defined (as
+                                  // opposed to __DYNAMIC__).
   unsigned PICLevel          : 2; // The value for __PIC__, if non-zero.
 
+  unsigned GNUInline         : 1; // Should GNU inline semantics be
+                                  // used (instead of C99 semantics).
+  unsigned NoInline          : 1; // Should __NO_INLINE__ be defined.
+
 private:
   unsigned GC : 2; // Objective-C Garbage Collection modes.  We declare
                    // this enum as unsigned because MSVC insists on making enums
@@ -114,8 +120,12 @@
     Optimize = 0;
     OptimizeSize = 0;
 
+    Static = 0;
     PICLevel = 0;
 
+    GNUInline = 0;
+    NoInline = 0;
+
     MainFileName = 0;
   }
   
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index 487e5c8..ddf1148 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -79,6 +79,11 @@
   CmdArgs.push_back("-main-file-name");
   CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
 
+  // Some flags which affect the language (via preprocessor
+  // defines). See darwin::CC1::AddCPPArgs.
+  if (Args.hasArg(options::OPT_static))
+    CmdArgs.push_back("-static-define");
+
   if (isa<AnalyzeJobAction>(JA)) {
     // Add default argument set.
     //
diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp
index d3d5b8c..452a04c 100644
--- a/lib/Lex/Preprocessor.cpp
+++ b/lib/Lex/Preprocessor.cpp
@@ -486,6 +486,10 @@
     DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
   else if (0) // STDC94 ?
     DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
+
+  // Standard conforming mode?
+  if (!PP.getLangOptions().GNUMode)
+    DefineBuiltinMacro(Buf, "__STRICT_ANSI__=1");
   
   if (PP.getLangOptions().CPlusPlus0x)
     DefineBuiltinMacro(Buf, "__GXX_EXPERIMENTAL_CXX0X__");
@@ -500,6 +504,7 @@
     if (PP.getLangOptions().ObjCNonFragileABI) {
       DefineBuiltinMacro(Buf, "__OBJC2__=1");
       DefineBuiltinMacro(Buf, "OBJC_ZEROCOST_EXCEPTIONS=1");
+      DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
     }
 
     if (PP.getLangOptions().getGCMode() != LangOptions::NonGC)
@@ -628,9 +633,20 @@
   
   // Build configuration options.  FIXME: these should be controlled by
   // command line options or something.
-  DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
   DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
-  DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
+
+  if (PP.getLangOptions().Static)
+    DefineBuiltinMacro(Buf, "__STATIC__=1");
+  else
+    DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
+
+  if (PP.getLangOptions().GNUInline)
+    DefineBuiltinMacro(Buf, "__GNUC_GNU_INLINE__=1");
+  else
+    DefineBuiltinMacro(Buf, "__GNUC_STDC_INLINE__=1");
+
+  if (PP.getLangOptions().NoInline)
+    DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
 
   if (unsigned PICLevel = PP.getLangOptions().PICLevel) {
     sprintf(MacroBuf, "__PIC__=%d", PICLevel);
diff --git a/test/Preprocessor/optimize.c b/test/Preprocessor/optimize.c
index 6de84c7..46df2a6 100644
--- a/test/Preprocessor/optimize.c
+++ b/test/Preprocessor/optimize.c
@@ -23,7 +23,7 @@
   #ifndef __OPTIMIZE__
     #error "__OPTIMIZE__ not defined"
   #endif
-  #ifndef __OPTIMIZE_SIZE__
+  #ifdef __OPTIMIZE_SIZE__
     #error "__OPTIMIZE_SIZE__ not defined"
   #endif
 #endif
diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp
index f2ccb80..13e4389 100644
--- a/tools/clang-cc/clang-cc.cpp
+++ b/tools/clang-cc/clang-cc.cpp
@@ -624,9 +624,10 @@
          llvm::cl::init(0));
 
 static llvm::cl::opt<unsigned>
-PICLevel("pic-level", llvm::cl::Prefix,
-         llvm::cl::desc("Value for __PIC__"),
-         llvm::cl::init(0));
+PICLevel("pic-level", llvm::cl::desc("Value for __PIC__"));
+
+static llvm::cl::opt<bool>
+StaticDefine("static-define", llvm::cl::desc("Should __STATIC__ be defined"));
 
 // FIXME: add:
 //   -fdollars-in-identifiers
@@ -771,16 +772,23 @@
   if (EmitAllDecls)
     Options.EmitAllDecls = 1;
 
-  if (OptSize)
-    Options.OptimizeSize = 1;
+  // The __OPTIMIZE_SIZE__ define is tied to -Oz, which we don't
+  // support.
+  Options.OptimizeSize = 0;
   
   // -Os implies -O2
-  if (Options.OptimizeSize || OptLevel)
+  if (OptSize || OptLevel)
     Options.Optimize = 1;
 
   assert(PICLevel <= 2 && "Invalid value for -pic-level");
   Options.PICLevel = PICLevel;
 
+  Options.GNUInline = !Options.C99;
+  // FIXME: This is affected by other options (-fno-inline). 
+  Options.NoInline = !OptSize && !OptLevel;
+
+  Options.Static = StaticDefine;
+
   if (MainFileName.getPosition())
     Options.setMainFileName(MainFileName.c_str());
 }
diff --git a/utils/builtin-defines.c b/utils/builtin-defines.c
new file mode 100644
index 0000000..98b0220
--- /dev/null
+++ b/utils/builtin-defines.c
@@ -0,0 +1,79 @@
+/* 
+This is a clang style test case for checking that preprocessor
+defines match gcc.
+*/
+
+/*
+RUN: for arch in -m32 -m64; do \
+RUN:   for lang in -std=gnu89 -ansi -std=c99 -std=gnu99; do \
+RUN:     for input in c objective-c; do \
+RUN:       for opts in "-O0" "-O1 -dynamic" "-O2 -static" "-Os"; do     \
+RUN:         echo "-- $arch, $lang, $input, $opts --"; \
+RUN:         for cc in 0 1; do \
+RUN:           if [ "$cc" == 0 ]; then \
+RUN:             cc_prog=clang; \
+RUN:             output=%t0; \
+RUN:           else \
+RUN:             cc_prog=gcc; \
+RUN:             output=%t1; \
+RUN:           fi; \
+RUN:           $cc_prog $arch $lang $opts -dM -E -x $input %s | sort > $output; \
+RUN:          done; \
+RUN:          if (! diff %t0 %t1); then exit 1; fi; \
+RUN:       done; \
+RUN:     done; \
+RUN:   done; \
+RUN: done;
+*/
+
+/* We don't care about this difference */
+#ifdef __PIC__
+#if __PIC__ == 1
+#undef __PIC__
+#undef __pic__
+#define __PIC__ 2
+#define __pic__ 2
+#endif
+#endif
+
+/* Undefine things we don't expect to match. */
+#undef __DEC_EVAL_METHOD__
+#undef __INT16_TYPE__
+#undef __INT32_TYPE__
+#undef __INT64_TYPE__
+#undef __INT8_TYPE__
+#undef __SSP__
+#undef __APPLE_CC__
+#undef __VERSION__
+#undef __clang__
+#undef __llvm__
+#undef __nocona
+#undef __nocona__
+#undef __k8
+#undef __k8__
+#undef __tune_nocona__
+#undef __tune_core2__
+#undef __POINTER_WIDTH__
+#undef __INTPTR_TYPE__
+
+#undef __DEC128_DEN__
+#undef __DEC128_EPSILON__
+#undef __DEC128_MANT_DIG__
+#undef __DEC128_MAX_EXP__
+#undef __DEC128_MAX__
+#undef __DEC128_MIN_EXP__
+#undef __DEC128_MIN__
+#undef __DEC32_DEN__
+#undef __DEC32_EPSILON__
+#undef __DEC32_MANT_DIG__
+#undef __DEC32_MAX_EXP__
+#undef __DEC32_MAX__
+#undef __DEC32_MIN_EXP__
+#undef __DEC32_MIN__
+#undef __DEC64_DEN__
+#undef __DEC64_EPSILON__
+#undef __DEC64_MANT_DIG__
+#undef __DEC64_MAX_EXP__
+#undef __DEC64_MAX__
+#undef __DEC64_MIN_EXP__
+#undef __DEC64_MIN__