From: Daniel Dunbar Date: Wed, 8 Apr 2009 03:03:23 +0000 (+0000) Subject: Set __PIC__ (more) correctly. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9fd0b1f845a61e71dd8099f596532d34c519630a;p=clang Set __PIC__ (more) correctly. - Add -pic-level clang-cc option to specify the value for the define, updated driver to pass this. - Added __pic__ - Added OBJC_ZEROCOST_EXCEPTIONS define while I was here (to match gcc). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68584 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/LangOptions.h b/include/clang/Basic/LangOptions.h index 495360ebef..f746edb65f 100644 --- a/include/clang/Basic/LangOptions.h +++ b/include/clang/Basic/LangOptions.h @@ -68,6 +68,8 @@ public: unsigned Optimize : 1; // Whether __OPTIMIZE__ should be defined. unsigned OptimizeSize : 1; // Whether __OPTIMIZE_SIZE__ should be // defined. + unsigned PICLevel : 2; // The value for __PIC__, if non-zero. + private: unsigned GC : 2; // Objective-C Garbage Collection modes. We declare // this enum as unsigned because MSVC insists on making enums @@ -106,6 +108,8 @@ public: Optimize = 0; OptimizeSize = 0; + + PICLevel = 0; } GCMode getGCMode() const { return (GCMode) GC; } diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index f44be0a7be..eb9bbf2b7b 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -124,6 +124,17 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("--relocation-model"); CmdArgs.push_back(Model); + // Infer the __PIC__ value. + // + // FIXME: This isn't quite right on Darwin, which always sets + // __PIC__=2. + if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) { + if (Args.hasArg(options::OPT_fPIC)) + CmdArgs.push_back("-pic-level=2"); + else + CmdArgs.push_back("-pic-level=1"); + } + if (Args.hasArg(options::OPT_ftime_report)) CmdArgs.push_back("--time-passes"); // FIXME: Set --enable-unsafe-fp-math. diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp index 139f310e7d..d3d5b8c3fd 100644 --- a/lib/Lex/Preprocessor.cpp +++ b/lib/Lex/Preprocessor.cpp @@ -497,8 +497,10 @@ static void InitializePredefinedMacros(Preprocessor &PP, if (PP.getLangOptions().ObjC1) { DefineBuiltinMacro(Buf, "__OBJC__=1"); - if (PP.getLangOptions().ObjCNonFragileABI) + if (PP.getLangOptions().ObjCNonFragileABI) { DefineBuiltinMacro(Buf, "__OBJC2__=1"); + DefineBuiltinMacro(Buf, "OBJC_ZEROCOST_EXCEPTIONS=1"); + } if (PP.getLangOptions().getGCMode() != LangOptions::NonGC) DefineBuiltinMacro(Buf, "__OBJC_GC__=1"); @@ -629,7 +631,14 @@ static void InitializePredefinedMacros(Preprocessor &PP, DefineBuiltinMacro(Buf, "__DYNAMIC__=1"); DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0"); DefineBuiltinMacro(Buf, "__NO_INLINE__=1"); - DefineBuiltinMacro(Buf, "__PIC__=1"); + + if (unsigned PICLevel = PP.getLangOptions().PICLevel) { + sprintf(MacroBuf, "__PIC__=%d", PICLevel); + DefineBuiltinMacro(Buf, MacroBuf); + + sprintf(MacroBuf, "__pic__=%d", PICLevel); + DefineBuiltinMacro(Buf, MacroBuf); + } // Macros to control C99 numerics and DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0"); diff --git a/test/Preprocessor/pic.c b/test/Preprocessor/pic.c new file mode 100644 index 0000000000..1a2300b1a1 --- /dev/null +++ b/test/Preprocessor/pic.c @@ -0,0 +1,10 @@ +// RUN: clang -static -dM -E -o %t %s && +// RUN: grep '#define __PIC__' %t | count 0 && +// RUN: grep '#define __pic__' %t | count 0 && +// RUN: clang -fpic -dM -E -o %t %s && +// RUN: grep '#define __PIC__ 1' %t | count 1 && +// RUN: grep '#define __pic__ 1' %t | count 1 && +// RUN: clang -fPIC -dM -E -o %t %s && +// RUN: grep '#define __PIC__ 2' %t | count 1 && +// RUN: grep '#define __pic__ 2' %t | count 1 && +// RUN: true diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp index 2843848f31..cca2e2cc14 100644 --- a/tools/clang-cc/clang-cc.cpp +++ b/tools/clang-cc/clang-cc.cpp @@ -620,6 +620,11 @@ OptLevel("O", llvm::cl::Prefix, llvm::cl::desc("Optimization level"), llvm::cl::init(0)); +static llvm::cl::opt +PICLevel("pic-level", llvm::cl::Prefix, + llvm::cl::desc("Value for __PIC__"), + llvm::cl::init(0)); + // FIXME: add: // -fdollars-in-identifiers static void InitializeLanguageStandard(LangOptions &Options, LangKind LK, @@ -769,6 +774,9 @@ static void InitializeLanguageStandard(LangOptions &Options, LangKind LK, // -Os implies -O2 if (Options.OptimizeSize || OptLevel) Options.Optimize = 1; + + assert(PICLevel <= 2 && "Invalid value for -pic-level"); + Options.PICLevel = PICLevel; } static llvm::cl::opt