[cfe-dev] diff: builtin macros

Vladimir Kirillov proger at uaoug.org.ua
Wed Sep 1 21:15:01 PDT 2010


Hello, cfe-dev!

There's a diff to support builtin macros defined at the preprocessor
initialization time (like __clang__, __i386__, etc) to be also treated
as builtin (MacroInfo::IsBuiltinMacro), so programs using clang
libraries would be able to distinct these macros from others properly.

I have added a new preprocessor keyword __define_builtin (i saw
__include_macros, so i guess its ok) to implement such feature.

If you suggest any better way of the implementation -- i'm also willing
to help then.


Thanks!
-------------- next part --------------
Index: include/clang/Basic/MacroBuilder.h
===================================================================
--- include/clang/Basic/MacroBuilder.h	(revision 112741)
+++ include/clang/Basic/MacroBuilder.h	(working copy)
@@ -25,8 +25,8 @@
   MacroBuilder(llvm::raw_ostream &Output) : Out(Output) {}
 
   /// Append a #define line for macro of the form "#define Name Value\n".
-  void defineMacro(const llvm::Twine &Name, const llvm::Twine &Value = "1") {
-    Out << "#define " << Name << ' ' << Value << '\n';
+  void defineMacro(const llvm::Twine &Name, const llvm::Twine &Value = "1", bool builtin = true) {
+    Out << (builtin ? "#__define_builtin " : "#define ") << Name << ' ' << Value << '\n';
   }
 
   /// Append a #undef line for Name.  Name should be of the form XXX
Index: include/clang/Basic/TokenKinds.def
===================================================================
--- include/clang/Basic/TokenKinds.def	(revision 112741)
+++ include/clang/Basic/TokenKinds.def	(working copy)
@@ -66,6 +66,7 @@
 
 // C99 6.10.3 - Macro Replacement.
 PPKEYWORD(define)
+PPKEYWORD(__define_builtin)
 PPKEYWORD(undef)
 
 // C99 6.10.4 - Line Control.
Index: include/clang/Lex/Preprocessor.h
===================================================================
--- include/clang/Lex/Preprocessor.h	(revision 112741)
+++ include/clang/Lex/Preprocessor.h	(working copy)
@@ -969,7 +969,7 @@
   void HandleImportDirective(Token &Tok);
 
   // Macro handling.
-  void HandleDefineDirective(Token &Tok);
+  void HandleDefineDirective(Token &Tok, bool isBuiltin = false);
   void HandleUndefDirective(Token &Tok);
 
   // Conditional Inclusion.
Index: lib/Frontend/InitPreprocessor.cpp
===================================================================
--- lib/Frontend/InitPreprocessor.cpp	(revision 112741)
+++ lib/Frontend/InitPreprocessor.cpp	(working copy)
@@ -40,10 +40,10 @@
     if (End != llvm::StringRef::npos)
       Diags.Report(diag::warn_fe_macro_contains_embedded_newline)
         << MacroName;
-    Builder.defineMacro(MacroName, MacroBody.substr(0, End));
+    Builder.defineMacro(MacroName, MacroBody.substr(0, End), false);
   } else {
     // Push "macroname 1".
-    Builder.defineMacro(Macro);
+    Builder.defineMacro(Macro, "1", false);
   }
 }
 
Index: lib/Basic/IdentifierTable.cpp
===================================================================
--- lib/Basic/IdentifierTable.cpp	(revision 112741)
+++ lib/Basic/IdentifierTable.cpp	(working copy)
@@ -187,6 +187,7 @@
   CASE(12, 'i', 'c', include_next);
 
   CASE(16, '_', 'i', __include_macros);
+  CASE(16, '_', 'd', __define_builtin);
 #undef CASE
 #undef HASH
   }
Index: lib/Lex/PPDirectives.cpp
===================================================================
--- lib/Lex/PPDirectives.cpp	(revision 112741)
+++ lib/Lex/PPDirectives.cpp	(working copy)
@@ -575,6 +575,8 @@
     // C99 6.10.3 - Macro Replacement.
     case tok::pp_define:
       return HandleDefineDirective(Result);
+    case tok::pp___define_builtin:
+      return HandleDefineDirective(Result, true);
     case tok::pp_undef:
       return HandleUndefDirective(Result);
 
@@ -1296,7 +1298,7 @@
 
 /// HandleDefineDirective - Implements #define.  This consumes the entire macro
 /// line then lets the caller lex the next real token.
-void Preprocessor::HandleDefineDirective(Token &DefineTok) {
+void Preprocessor::HandleDefineDirective(Token &DefineTok, bool isBuiltin) {
   ++NumDefined;
 
   Token MacroNameTok;
@@ -1487,6 +1489,9 @@
     ReleaseMacroInfo(OtherMI);
   }
 
+  // set IsBuiltinMacro flag for internal builtin macros
+  MI->setIsBuiltinMacro(isBuiltin);
+
   setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
 
   // If the callbacks want to know, tell them about the macro definition.


More information about the cfe-dev mailing list