diff options
author | Anders Carlsson <andersca@mac.com> | 2009-04-06 17:37:10 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2009-04-06 17:37:10 +0000 |
commit | 4ca076fffdfa72ebfab364c8de091b89fc291ae6 (patch) | |
tree | 0dd4e233aad1da37a625e3a7c6e46c4dfc266e3d | |
parent | 50748f4362b334e4b88b1f8397bb2f63269a6bf1 (diff) |
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
-rw-r--r-- | include/clang/Basic/LangOptions.h | 6 | ||||
-rw-r--r-- | lib/Lex/Preprocessor.cpp | 5 | ||||
-rw-r--r-- | test/Preprocessor/optimize.c | 29 | ||||
-rw-r--r-- | tools/clang-cc/clang-cc.cpp | 59 |
4 files changed, 74 insertions, 25 deletions
diff --git a/include/clang/Basic/LangOptions.h b/include/clang/Basic/LangOptions.h index 6486be25ab..495360ebef 100644 --- a/include/clang/Basic/LangOptions.h +++ b/include/clang/Basic/LangOptions.h @@ -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; } diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp index a43bb6427e..9eaf1403a2 100644 --- a/lib/Lex/Preprocessor.cpp +++ b/lib/Lex/Preprocessor.cpp @@ -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 index 0000000000..55df0c1196 --- /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 5a713e4612..278b430658 100644 --- a/tools/clang-cc/clang-cc.cpp +++ b/tools/clang-cc/clang-cc.cpp @@ -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)")); |