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

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:26:28 PDT 2007


Author: sabre
Date: Wed Jul 11 11:26:28 2007
New Revision: 38980

URL: http://llvm.org/viewvc/llvm-project?rev=38980&view=rev
Log:
Implement the #define_target preprocessor directive.

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

Modified: cfe/cfe/trunk/Lex/MacroInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/MacroInfo.cpp?rev=38980&r1=38979&r2=38980&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/MacroInfo.cpp (original)
+++ cfe/cfe/trunk/Lex/MacroInfo.cpp Wed Jul 11 11:26:28 2007
@@ -22,6 +22,7 @@
   IsC99Varargs = false;
   IsGNUVarargs = false;
   IsBuiltinMacro = false;
+  IsTargetSpecific = false;
   IsDisabled = false;
   IsUsed = true;
 }
@@ -29,6 +30,9 @@
 /// isIdenticalTo - Return true if the specified macro definition is equal to
 /// this macro in spelling, arguments, and whitespace.  This is used to emit
 /// duplicate definition warnings.  This implements the rules in C99 6.10.3.
+///
+/// Note that this intentionally does not check isTargetSpecific for matching.
+///
 bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
   // Check # tokens in replacement, number of args, and various flags all match.
   if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||

Modified: cfe/cfe/trunk/Lex/PPExpressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/PPExpressions.cpp?rev=38980&r1=38979&r2=38980&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/PPExpressions.cpp (original)
+++ cfe/cfe/trunk/Lex/PPExpressions.cpp Wed Jul 11 11:26:28 2007
@@ -21,6 +21,7 @@
 
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Lex/MacroInfo.h"
+#include "clang/Basic/TargetInfo.h"
 #include "clang/Basic/TokenKinds.h"
 #include "clang/Basic/Diagnostic.h"
 using namespace llvm;
@@ -95,7 +96,17 @@
     Result = II->getMacroInfo() != 0;
     
     // If there is a macro, mark it used.
-    if (Result) II->getMacroInfo()->setIsUsed(true);
+    if (Result) {
+      II->getMacroInfo()->setIsUsed(true);
+      
+      // If this is the first use of a target-specific macro, warn about it.
+      if (II->getMacroInfo()->isTargetSpecific()) {
+        // Don't warn on second use.
+        II->getMacroInfo()->setIsTargetSpecific(false);
+        PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(),
+                                                  diag::port_target_macro_use);
+      }
+    }
 
     // Consume identifier.
     PP.Lex(PeekTok);

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

==============================================================================
--- cfe/cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/cfe/trunk/Lex/Preprocessor.cpp Wed Jul 11 11:26:28 2007
@@ -32,6 +32,7 @@
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Basic/TargetInfo.h"
 #include "llvm/ADT/SmallVector.h"
 #include <iostream>
 using namespace llvm;
@@ -592,6 +593,13 @@
     return false;
   }
   
+  // If this is the first use of a target-specific macro, warn about it.
+  if (MI->isTargetSpecific()) {
+    MI->setIsTargetSpecific(false);  // Don't warn on second use.
+    getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(),
+                                           diag::port_target_macro_use);
+  }
+  
   /// Args - If this is a function-like macro expansion, this contains,
   /// for each macro argument, the list of tokens that were provided to the
   /// invocation.
@@ -1428,7 +1436,7 @@
       break;
     case 6:
       if (Directive[0] == 'd' && !strcmp(Directive, "define"))
-        return HandleDefineDirective(Result);
+        return HandleDefineDirective(Result, false);
       if (Directive[0] == 'i' && !strcmp(Directive, "ifndef"))
         return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
       if (Directive[0] == 'i' && !strcmp(Directive, "import"))
@@ -1455,6 +1463,10 @@
       if (Directive[0] == 'i' && !strcmp(Directive, "include_next"))
         return HandleIncludeNextDirective(Result);      // Handle #include_next.
       break;
+    case 13:
+      if (Directive[0] == 'd' && !strcmp(Directive, "define_target"))
+        return HandleDefineDirective(Result, true);
+      break;
     }
     break;
   }
@@ -1693,9 +1705,11 @@
 }
 
 /// HandleDefineDirective - Implements #define.  This consumes the entire macro
-/// line then lets the caller lex the next real token.
+/// line then lets the caller lex the next real token.  If 'isTargetSpecific' is
+/// true, then this is a "#define_target", otherwise this is a "#define".
 ///
-void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) {
+void Preprocessor::HandleDefineDirective(LexerToken &DefineTok,
+                                         bool isTargetSpecific) {
   ++NumDefined;
 
   LexerToken MacroNameTok;
@@ -1710,6 +1724,7 @@
   CurLexer->KeepCommentMode = Features.KeepMacroComments;
   
   MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
+  if (isTargetSpecific) MI->setIsTargetSpecific();
   
   LexerToken Tok;
   LexUnexpandedToken(Tok);
@@ -1897,8 +1912,18 @@
   
   MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
 
-  // If there is a macro, mark it used.
-  if (MI) MI->setIsUsed(true);
+  // If there is a macro, process it.
+  if (MI) {
+    // Mark it used.
+    MI->setIsUsed(true);
+
+    // If this is the first use of a target-specific macro, warn about it.
+    if (MI->isTargetSpecific()) {
+      MI->setIsTargetSpecific(false);  // Don't warn on second use.
+      getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(),
+                                             diag::port_target_macro_use);
+    }
+  }
   
   // Should we include the stuff contained by this directive?
   if (!MI == isIfndef) {

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=38980&r1=38979&r2=38980&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def Wed Jul 11 11:26:28 2007
@@ -24,6 +24,9 @@
 // Portability
 //===----------------------------------------------------------------------===//
 
+DIAG(port_target_macro_use, NOTE,
+     "use of a target-specific macro, source is not 'portable'")
+
 DIAG(port_wchar_t, NOTE,
      "sizeof(wchar_t) varies between targets, source is not 'portable'")
 

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

==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/TargetInfo.h (original)
+++ cfe/cfe/trunk/include/clang/Basic/TargetInfo.h Wed Jul 11 11:26:28 2007
@@ -85,7 +85,12 @@
   }
   
   ///===---- Target property query methods --------------------------------===//
-  
+
+  /// DiagnoseNonPortability - Emit a diagnostic indicating that the current
+  /// translation unit is non-portable due to a construct at the specified
+  /// location.  DiagKind indicates what went wrong.
+  void DiagnoseNonPortability(SourceLocation Loc, unsigned DiagKind);
+
   /// getTargetDefines - Appends the target-specific #define values for this
   /// target set to the specified buffer.
   void getTargetDefines(std::vector<char> &DefineBuffer);
@@ -96,8 +101,8 @@
     if (!WCharWidth) ComputeWCharWidth(Loc);
     return WCharWidth;
   }
+
 private:
-  void DiagnoseNonPortability(SourceLocation Loc, unsigned DiagKind);
   void ComputeWCharWidth(SourceLocation Loc);
 };
 

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=38980&r1=38979&r2=38980&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/MacroInfo.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/MacroInfo.h Wed Jul 11 11:26:28 2007
@@ -57,6 +57,9 @@
   /// it has not yet been redefined or undefined.
   bool IsBuiltinMacro : 1;
   
+  /// IsTargetSpecific - True if this is a target-specific macro defined with
+  /// #define_target.
+  bool IsTargetSpecific : 1;
 private:
   //===--------------------------------------------------------------------===//
   // State that changes as the macro is used.
@@ -88,6 +91,13 @@
     IsBuiltinMacro = Val;
   }
   
+  /// setIsTargetSpecific - Set or clear the IsTargetSpecific flag.
+  ///
+  void setIsTargetSpecific(bool Val = true) {
+    IsTargetSpecific = Val;
+  }
+  bool isTargetSpecific() const { return IsTargetSpecific; }
+  
   /// setIsUsed - Set the value of the IsUsed flag.
   ///
   void setIsUsed(bool Val) {

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=38980&r1=38979&r2=38980&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/Preprocessor.h Wed Jul 11 11:26:28 2007
@@ -532,7 +532,7 @@
   void HandleImportDirective(LexerToken &Tok);
   
   // Macro handling.
-  void HandleDefineDirective(LexerToken &Tok);
+  void HandleDefineDirective(LexerToken &Tok, bool isTargetSpecific);
   void HandleUndefDirective(LexerToken &Tok);
   // HandleAssertDirective(LexerToken &Tok);
   // HandleUnassertDirective(LexerToken &Tok);





More information about the cfe-commits mailing list