[cfe-commits] r68584 - in /cfe/trunk: include/clang/Basic/LangOptions.h lib/Driver/Tools.cpp lib/Lex/Preprocessor.cpp test/Preprocessor/pic.c tools/clang-cc/clang-cc.cpp

Daniel Dunbar daniel at zuster.org
Tue Apr 7 20:03:27 PDT 2009


Author: ddunbar
Date: Tue Apr  7 22:03:23 2009
New Revision: 68584

URL: http://llvm.org/viewvc/llvm-project?rev=68584&view=rev
Log:
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).

Added:
    cfe/trunk/test/Preprocessor/pic.c
Modified:
    cfe/trunk/include/clang/Basic/LangOptions.h
    cfe/trunk/lib/Driver/Tools.cpp
    cfe/trunk/lib/Lex/Preprocessor.cpp
    cfe/trunk/tools/clang-cc/clang-cc.cpp

Modified: cfe/trunk/include/clang/Basic/LangOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/LangOptions.h?rev=68584&r1=68583&r2=68584&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/LangOptions.h (original)
+++ cfe/trunk/include/clang/Basic/LangOptions.h Tue Apr  7 22:03:23 2009
@@ -68,6 +68,8 @@
   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 @@
     
     Optimize = 0;
     OptimizeSize = 0;
+
+    PICLevel = 0;
   }
   
   GCMode getGCMode() const { return (GCMode) GC; }

Modified: cfe/trunk/lib/Driver/Tools.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.cpp?rev=68584&r1=68583&r2=68584&view=diff

==============================================================================
--- cfe/trunk/lib/Driver/Tools.cpp (original)
+++ cfe/trunk/lib/Driver/Tools.cpp Tue Apr  7 22:03:23 2009
@@ -124,6 +124,17 @@
     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.

Modified: cfe/trunk/lib/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Preprocessor.cpp?rev=68584&r1=68583&r2=68584&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/lib/Lex/Preprocessor.cpp Tue Apr  7 22:03:23 2009
@@ -497,8 +497,10 @@
   
   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 @@
   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 <float.h>
   DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0");

Added: cfe/trunk/test/Preprocessor/pic.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/pic.c?rev=68584&view=auto

==============================================================================
--- cfe/trunk/test/Preprocessor/pic.c (added)
+++ cfe/trunk/test/Preprocessor/pic.c Tue Apr  7 22:03:23 2009
@@ -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

Modified: cfe/trunk/tools/clang-cc/clang-cc.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-cc/clang-cc.cpp?rev=68584&r1=68583&r2=68584&view=diff

==============================================================================
--- cfe/trunk/tools/clang-cc/clang-cc.cpp (original)
+++ cfe/trunk/tools/clang-cc/clang-cc.cpp Tue Apr  7 22:03:23 2009
@@ -620,6 +620,11 @@
          llvm::cl::desc("Optimization level"),
          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));
+
 // FIXME: add:
 //   -fdollars-in-identifiers
 static void InitializeLanguageStandard(LangOptions &Options, LangKind LK,
@@ -769,6 +774,9 @@
   // -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<bool>





More information about the cfe-commits mailing list