]> granicus.if.org Git - clang/commitdiff
Define __OPTIMIZE__ and __OPTIMIZE_SIZE__ if the -O[12] and -Os flags are passed...
authorAnders Carlsson <andersca@mac.com>
Mon, 6 Apr 2009 17:37:10 +0000 (17:37 +0000)
committerAnders Carlsson <andersca@mac.com>
Mon, 6 Apr 2009 17:37:10 +0000 (17:37 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68450 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/LangOptions.h
lib/Lex/Preprocessor.cpp
test/Preprocessor/optimize.c [new file with mode: 0644]
tools/clang-cc/clang-cc.cpp

index 6486be25ab1e146d822da892321cbab6373cf776..495360ebeffe8985b7d491541519f4623adb765f 100644 (file)
@@ -65,6 +65,9 @@ public:
   unsigned HeinousExtensions : 1; // Extensions that we really don't like and
                                   // may be ripped out at any time.
 
+  unsigned Optimize          : 1; // Whether __OPTIMIZE__ should be defined.
+  unsigned OptimizeSize      : 1; // Whether __OPTIMIZE_SIZE__ should be 
+                                  // defined.
 private:
   unsigned GC : 2; // Objective-C Garbage Collection modes.  We declare
                    // this enum as unsigned because MSVC insists on making enums
@@ -100,6 +103,9 @@ public:
     OverflowChecking = 0;
 
     InstantiationDepth = 99;
+    
+    Optimize = 0;
+    OptimizeSize = 0;
   }
   
   GCMode getGCMode() const { return (GCMode) GC; }
index a43bb6427e90504302dc5ae0f9d3acd6384e2039..9eaf1403a2d7e8896036b1f9ab1528ab85ef0951 100644 (file)
@@ -547,6 +547,11 @@ static void InitializePredefinedMacros(Preprocessor &PP,
     DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__");
   }
   
+  if (PP.getLangOptions().Optimize)
+    DefineBuiltinMacro(Buf, "__OPTIMIZE__=1");
+  if (PP.getLangOptions().OptimizeSize)
+    DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1");
+    
   // Initialize target-specific preprocessor defines.
   const TargetInfo &TI = PP.getTargetInfo();
   
diff --git a/test/Preprocessor/optimize.c b/test/Preprocessor/optimize.c
new file mode 100644 (file)
index 0000000..55df0c1
--- /dev/null
@@ -0,0 +1,29 @@
+// RUN: clang-cc -Eonly optimize.c -DOPT_O2 -O2 -verify &&
+#ifdef OPT_O2
+  #ifndef __OPTIMIZE__
+    #error "__OPTIMIZE__ not defined"
+  #endif
+  #ifdef __OPTIMIZE_SIZE
+    #error "__OPTIMIZE_SIZE__ defined"
+  #endif
+#endif
+
+// RUN: clang-cc -Eonly optimize.c -DOPT_O0 -O0 -verify &&
+#ifdef OPT_O0
+  #ifdef __OPTIMIZE__
+    #error "__OPTIMIZE__ defined"
+  #endif
+  #ifdef __OPTIMIZE_SIZE
+    #error "__OPTIMIZE_SIZE__ defined"
+  #endif
+#endif
+
+// RUN: clang-cc -Eonly optimize.c -DOPT_OS -Os -verify
+#ifdef OPT_OS
+  #ifndef __OPTIMIZE__
+    #error "__OPTIMIZE__ not defined"
+  #endif
+  #ifndef __OPTIMIZE_SIZE
+    #error "__OPTIMIZE_SIZE__ not defined"
+  #endif
+#endif
index 5a713e4612cdc3f32edcd92ba25494af232d33bb..278b430658c69ac2ea4f8f38d5d5d3c0d6734f29 100644 (file)
@@ -601,6 +601,33 @@ TemplateDepth("ftemplate-depth", llvm::cl::init(99),
               llvm::cl::desc("Maximum depth of recursive template "
                              "instantiation"));
 
+
+static llvm::cl::opt<bool>
+OptSize("Os", llvm::cl::desc("Optimize for size"));
+
+static llvm::cl::opt<bool>
+NoCommon("fno-common",
+         llvm::cl::desc("Compile common globals like normal definitions"),
+         llvm::cl::ValueDisallowed);
+
+
+// It might be nice to add bounds to the CommandLine library directly.
+struct OptLevelParser : public llvm::cl::parser<unsigned> {
+  bool parse(llvm::cl::Option &O, const char *ArgName,
+             const std::string &Arg, unsigned &Val) {
+    if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val))
+      return true;
+    // FIXME: Support -O4.
+    if (Val > 3)
+      return O.error(": '" + Arg + "' invalid optimization level!");
+    return false;
+  }
+};
+static llvm::cl::opt<unsigned, false, OptLevelParser>
+OptLevel("O", llvm::cl::Prefix,
+         llvm::cl::desc("Optimization level"),
+         llvm::cl::init(0));
+
 // FIXME: add:
 //   -fdollars-in-identifiers
 static void InitializeLanguageStandard(LangOptions &Options, LangKind LK,
@@ -746,6 +773,13 @@ static void InitializeLanguageStandard(LangOptions &Options, LangKind LK,
 
   if (EmitAllDecls)
     Options.EmitAllDecls = 1;
+
+  if (OptSize)
+    Options.OptimizeSize = 1;
+  
+  // -Os implies -O2
+  if (Options.OptimizeSize || OptLevel)
+    Options.Optimize = 1;
 }
 
 static llvm::cl::opt<bool>
@@ -1308,31 +1342,6 @@ static llvm::cl::opt<bool>
 GenerateDebugInfo("g",
                   llvm::cl::desc("Generate source level debug information"));
 
-static llvm::cl::opt<bool>
-OptSize("Os", llvm::cl::desc("Optimize for size"));
-
-static llvm::cl::opt<bool>
-NoCommon("fno-common",
-         llvm::cl::desc("Compile common globals like normal definitions"),
-         llvm::cl::ValueDisallowed);
-
-// It might be nice to add bounds to the CommandLine library directly.
-struct OptLevelParser : public llvm::cl::parser<unsigned> {
-  bool parse(llvm::cl::Option &O, const char *ArgName,
-             const std::string &Arg, unsigned &Val) {
-    if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val))
-      return true;
-    // FIXME: Support -O4.
-    if (Val > 3)
-      return O.error(": '" + Arg + "' invalid optimization level!");
-    return false;
-  }
-};
-static llvm::cl::opt<unsigned, false, OptLevelParser>
-OptLevel("O", llvm::cl::Prefix,
-         llvm::cl::desc("Optimization level"),
-         llvm::cl::init(0));
-
 static llvm::cl::opt<std::string>
 TargetCPU("mcpu",
          llvm::cl::desc("Target a specific cpu type (-mcpu=help for details)"));