Define __OPTIMIZE__ and __OPTIMIZE_SIZE__ if the -O[12] and -Os flags are passed to the compiler.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68450 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/LangOptions.h b/include/clang/Basic/LangOptions.h
index 6486be2..495360e 100644
--- a/include/clang/Basic/LangOptions.h
+++ b/include/clang/Basic/LangOptions.h
@@ -65,6 +65,9 @@
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 @@
OverflowChecking = 0;
InstantiationDepth = 99;
+
+ Optimize = 0;
+ OptimizeSize = 0;
}
GCMode getGCMode() const { return (GCMode) GC; }
diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp
index a43bb64..9eaf140 100644
--- a/lib/Lex/Preprocessor.cpp
+++ b/lib/Lex/Preprocessor.cpp
@@ -547,6 +547,11 @@
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
index 0000000..55df0c1
--- /dev/null
+++ b/test/Preprocessor/optimize.c
@@ -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
diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp
index 5a713e4..278b430 100644
--- a/tools/clang-cc/clang-cc.cpp
+++ b/tools/clang-cc/clang-cc.cpp
@@ -601,6 +601,33 @@
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 @@
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 @@
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)"));