[cfe-commits] r38586 - in /cfe/cfe/trunk: Lex/Preprocessor.cpp include/clang/Basic/DiagnosticKinds.def include/clang/Lex/MacroInfo.h include/clang/Lex/Preprocessor.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:22:57 PDT 2007


Author: sabre
Date: Wed Jul 11 11:22:57 2007
New Revision: 38586

URL: http://llvm.org/viewvc/llvm-project?rev=38586&view=rev
Log:
Remove dead variables.
Add initial support for builtin macros, including warning if they are defined or undefined.
Register __LINE__ as a builtin macro.

Modified:
    cfe/cfe/trunk/Lex/Preprocessor.cpp
    cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/cfe/trunk/include/clang/Lex/MacroInfo.h
    cfe/cfe/trunk/include/clang/Lex/Preprocessor.h

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

==============================================================================
--- cfe/cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/cfe/trunk/Lex/Preprocessor.cpp Wed Jul 11 11:22:57 2007
@@ -69,6 +69,9 @@
   // Initialize the pragma handlers.
   PragmaHandlers = new PragmaNamespace(0);
   RegisterBuiltinPragmas();
+  
+  // Initialize builtin macros like __LINE__ and friends.
+  RegisterBuiltinMacros();
 }
 
 Preprocessor::~Preprocessor() {
@@ -402,40 +405,25 @@
   CurMacroExpander = new MacroExpander(Tok, *this);
 }
 
-
 //===----------------------------------------------------------------------===//
-// Lexer Event Handling.
+// Macro Expansion Handling.
 //===----------------------------------------------------------------------===//
 
-/// HandleIdentifier - This callback is invoked when the lexer reads an
-/// identifier.  This callback looks up the identifier in the map and/or
-/// potentially macro expands it or turns it into a named token (like 'for').
-void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
-  if (Identifier.getIdentifierInfo() == 0) {
-    // If we are skipping tokens (because we are in a #if 0 block), there will
-    // be no identifier info, just return the token.
-    assert(isSkipping() && "Token isn't an identifier?");
-    return;
-  }
-  IdentifierTokenInfo &ITI = *Identifier.getIdentifierInfo();
-
-  // If this identifier was poisoned, and if it was not produced from a macro
-  // expansion, emit an error.
-  if (ITI.isPoisoned() && CurLexer)
-    Diag(Identifier, diag::err_pp_used_poisoned_id);
-  
-  if (MacroInfo *MI = ITI.getMacroInfo())
-    if (MI->isEnabled() && !DisableMacroExpansion)
-      return HandleMacroExpandedIdentifier(Identifier, MI);
-
-  // Change the kind of this identifier to the appropriate token kind, e.g.
-  // turning "for" into a keyword.
-  Identifier.SetKind(ITI.getTokenID());
-    
-  // If this is an extension token, diagnose its use.
-  if (ITI.isExtensionToken()) Diag(Identifier, diag::ext_token_used);
+/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
+/// identifier table.
+void Preprocessor::RegisterBuiltinMacros() {
+  // Do this for each thing.
+  MacroInfo *MI = new MacroInfo(SourceLocation());
+  MI->setIsBuiltinMacro();
+  getIdentifierInfo("__LINE__")->setMacroInfo(MI);
+
+  // FIXME: Warn on #undef / #define of a builtin macro.
+  // FIXME: make HandleMacroExpandedIdentifier handle this case.
+  // FIXME: implement them all, including _Pragma.
+  //MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
 }
 
+
 /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
 /// expanded as a macro, handle it and return the next token as 'Identifier'.
 void Preprocessor::HandleMacroExpandedIdentifier(LexerToken &Identifier, 
@@ -511,6 +499,40 @@
   return Lex(Identifier);
 }
 
+
+//===----------------------------------------------------------------------===//
+// Lexer Event Handling.
+//===----------------------------------------------------------------------===//
+
+/// HandleIdentifier - This callback is invoked when the lexer reads an
+/// identifier.  This callback looks up the identifier in the map and/or
+/// potentially macro expands it or turns it into a named token (like 'for').
+void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
+  if (Identifier.getIdentifierInfo() == 0) {
+    // If we are skipping tokens (because we are in a #if 0 block), there will
+    // be no identifier info, just return the token.
+    assert(isSkipping() && "Token isn't an identifier?");
+    return;
+  }
+  IdentifierTokenInfo &ITI = *Identifier.getIdentifierInfo();
+
+  // If this identifier was poisoned, and if it was not produced from a macro
+  // expansion, emit an error.
+  if (ITI.isPoisoned() && CurLexer)
+    Diag(Identifier, diag::err_pp_used_poisoned_id);
+  
+  if (MacroInfo *MI = ITI.getMacroInfo())
+    if (MI->isEnabled() && !DisableMacroExpansion)
+      return HandleMacroExpandedIdentifier(Identifier, MI);
+
+  // Change the kind of this identifier to the appropriate token kind, e.g.
+  // turning "for" into a keyword.
+  Identifier.SetKind(ITI.getTokenID());
+    
+  // If this is an extension token, diagnose its use.
+  if (ITI.isExtensionToken()) Diag(Identifier, diag::ext_token_used);
+}
+
 /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
 /// the current file.  This either returns the EOF token or pops a level off
 /// the include stack and keeps going.
@@ -790,9 +812,6 @@
           DiscardUntilEndOfDirective();
           ShouldEnter = false;
         } else {
-          // Evaluate the #elif condition!
-          const char *Start = CurLexer->BufferPtr;
-
           // Restore the value of SkippingContents so that identifiers are
           // looked up, etc, inside the #elif expression.
           assert(SkippingContents && "We have to be skipping here!");
@@ -1096,6 +1115,10 @@
   // Finally, if this identifier already had a macro defined for it, verify that
   // the macro bodies are identical and free the old definition.
   if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
+    if (OtherMI->isBuiltinMacro())
+      Diag(MacroNameTok, diag::pp_redef_builtin_macro);
+    
+    
     // FIXME: Verify the definition is the same.
     // Macros must be identical.  This means all tokes and whitespace separation
     // must be the same.
@@ -1125,6 +1148,9 @@
   
   // If the macro is not defined, this is a noop undef, just return.
   if (MI == 0) return;
+
+  if (MI->isBuiltinMacro())
+    Diag(MacroNameTok, diag::pp_undef_builtin_macro);
   
 #if 0 // FIXME: implement warn_unused_macros.
   if (CPP_OPTION (pfile, warn_unused_macros))
@@ -1175,8 +1201,6 @@
 ///
 void Preprocessor::HandleIfDirective(LexerToken &IfToken) {
   ++NumIf;
-  const char *Start = CurLexer->BufferPtr;
-
   bool ConditionalTrue = EvaluateDirectiveExpression();
   
   // Should we include the stuff contained by this directive?

Modified: cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=38586&r1=38585&r2=38586&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def Wed Jul 11 11:22:57 2007
@@ -94,6 +94,10 @@
      "poisoning existing macro")
 DIAG(pp_out_of_date_dependency, WARNING,
      "current file is older than dependency %s")
+DIAG(pp_undef_builtin_macro, WARNING,
+     "undefining builtin macro")
+DIAG(pp_redef_builtin_macro, WARNING,
+     "redefining builtin macro")
 
 DIAG(ext_pp_import_directive, EXTENSION,
      "#import is a language extension")

Modified: cfe/cfe/trunk/include/clang/Lex/MacroInfo.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Lex/MacroInfo.h?rev=38586&r1=38585&r2=38586&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/MacroInfo.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/MacroInfo.h Wed Jul 11 11:22:57 2007
@@ -36,12 +36,26 @@
   /// isDisabled - True if we have started an expansion of this macro already.
   /// This disbles recursive expansion, which would be quite bad for things like
   /// #define A A.
-  bool isDisabled;
+  bool IsDisabled : 1;
   
+  /// isBuiltinMacro - True if this is a builtin macro, such as __LINE__, and if
+  /// it has not yet been redefined or undefined.
+  bool IsBuiltinMacro : 1;
 public:
   MacroInfo(SourceLocation DefLoc) : Location(DefLoc) {
-    isDisabled = false;
+    IsDisabled = false;
+    IsBuiltinMacro = false;
   }
+  
+  /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag.
+  ///
+  void setIsBuiltinMacro(bool Val = true) {
+    IsBuiltinMacro = Val;
+  }
+  
+  /// isBuiltinMacro - Return true if this macro is a builtin macro, such as
+  /// __LINE__, which requires processing before expansion.
+  bool isBuiltinMacro() const { return IsBuiltinMacro; }
 
   /// getNumTokens - Return the number of tokens that this macro expands to.
   ///
@@ -62,16 +76,16 @@
   
   /// isEnabled - Return true if this macro is enabled: in other words, that we
   /// are not currently in an expansion of this macro.
-  bool isEnabled() const { return !isDisabled; }
+  bool isEnabled() const { return !IsDisabled; }
   
   void EnableMacro() {
-    assert(isDisabled && "Cannot enable an already-enabled macro!");
-    isDisabled = false;
+    assert(IsDisabled && "Cannot enable an already-enabled macro!");
+    IsDisabled = false;
   }
 
   void DisableMacro() {
-    assert(!isDisabled && "Cannot disable an already-disabled macro!");
-    isDisabled = true;
+    assert(!IsDisabled && "Cannot disable an already-disabled macro!");
+    IsDisabled = true;
   }
 };
     

Modified: cfe/cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=38586&r1=38585&r2=38586&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/Preprocessor.h Wed Jul 11 11:22:57 2007
@@ -401,6 +401,9 @@
   /// #pragma GCC poison/system_header/dependency and #pragma once.
   void RegisterBuiltinPragmas();
   
+  /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
+  /// identifier table.
+  void RegisterBuiltinMacros();
   
   /// HandleMacroExpandedIdentifier - If an identifier token is read that is to
   /// be expanded as a macro, handle it and return the next token as 'Tok'.





More information about the cfe-commits mailing list