[cfe-commits] r38580 - in /cfe/cfe/trunk: Lex/MacroExpander.cpp Lex/Preprocessor.cpp include/clang/Lex/MacroExpander.h

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


Author: sabre
Date: Wed Jul 11 11:22:53 2007
New Revision: 38580

URL: http://llvm.org/viewvc/llvm-project?rev=38580&view=rev
Log:
Implement Preprocessor/macro_expandloc.c by giving the optimized macro
expansion case a correct source location.

Modified:
    cfe/cfe/trunk/Lex/MacroExpander.cpp
    cfe/cfe/trunk/Lex/Preprocessor.cpp
    cfe/cfe/trunk/include/clang/Lex/MacroExpander.h

Modified: cfe/cfe/trunk/Lex/MacroExpander.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/MacroExpander.cpp?rev=38580&r1=38579&r2=38580&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/MacroExpander.cpp (original)
+++ cfe/cfe/trunk/Lex/MacroExpander.cpp Wed Jul 11 11:22:53 2007
@@ -25,6 +25,27 @@
     HasLeadingSpace(Tok.hasLeadingSpace()) {
 }
 
+
+/// getInstantiationLoc - Return a SourceLocation that specifies a macro
+/// instantiation whose physical location is PhysLoc but the logical location
+/// is InstantiationLoc.
+SourceLocation MacroExpander::
+getInstantiationLoc(Preprocessor &PP,
+                    SourceLocation PhysLoc, SourceLocation InstantiationLoc) {
+  // The token's current location indicate where the token was lexed from.  We
+  // need this information to compute the spelling of the token, but any
+  // diagnostics for the expanded token should appear as if they came from
+  // InstantiationLoc.  Pull this information together into a new SourceLocation
+  // that captures all of this.
+  unsigned CharFilePos = PhysLoc.getRawFilePos();
+  unsigned CharFileID  = PhysLoc.getFileID();
+  
+  unsigned InstantiationFileID =
+    PP.getSourceManager().createFileIDForMacroExp(InstantiationLoc, CharFileID);
+  return SourceLocation(InstantiationFileID, CharFilePos);
+}
+
+
 /// Lex - Lex and return a token from this macro stream.
 ///
 void MacroExpander::Lex(LexerToken &Tok) {
@@ -34,19 +55,10 @@
   
   // Get the next token to return.
   Tok = Macro.getReplacementToken(CurToken++);
-  //Tok.SetLocation(InstantiateLoc);
 
-  // The token's current location indicate where the token was lexed from.  We
-  // need this information to compute the spelling of the token, but any
-  // diagnostics for the expanded token should appear as if they came from
-  // InstantiateLoc.  Pull this information together into a new SourceLocation
-  // that captures all of this.
-  unsigned CharFilePos = Tok.getLocation().getRawFilePos();
-  unsigned CharFileID  = Tok.getLocation().getFileID();
-  
-  unsigned InstantiationFileID =
-    PP.getSourceManager().createFileIDForMacroExp(InstantiateLoc, CharFileID);
-  Tok.SetLocation(SourceLocation(InstantiationFileID, CharFilePos));
+  // Update the tokens location to include both its logical and physical
+  // locations.
+  Tok.SetLocation(getInstantiationLoc(PP, Tok.getLocation(), InstantiateLoc));
 
   // If this is the first token, set the lexical properties of the token to
   // match the lexical properties of the macro identifier.

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

==============================================================================
--- cfe/cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/cfe/trunk/Lex/Preprocessor.cpp Wed Jul 11 11:22:53 2007
@@ -466,15 +466,23 @@
         bool isAtStartOfLine = Identifier.isAtStartOfLine();
         bool hasLeadingSpace = Identifier.hasLeadingSpace();
 
+        // Remember where the token is instantiated.
+        SourceLocation InstantiateLoc = Identifier.getLocation();
+        
         // Replace the result token.
         Identifier = MI->getReplacementToken(0);
 
         // Restore the StartOfLine/LeadingSpace markers.
         Identifier.SetFlagValue(LexerToken::StartOfLine , isAtStartOfLine);
         Identifier.SetFlagValue(LexerToken::LeadingSpace, hasLeadingSpace);
-        
-        // FIXME: Get correct macro expansion stack location info!
-        
+
+        // Update the tokens location to include both its logical and physical
+        // locations.
+        SourceLocation Loc =
+          MacroExpander::getInstantiationLoc(*this, Identifier.getLocation(),
+                                             InstantiateLoc);
+        Identifier.SetLocation(Loc);
+
         // Since this is not an identifier token, it can't be macro expanded, so
         // we're done.
         ++NumFastMacroExpanded;

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

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/MacroExpander.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/MacroExpander.h Wed Jul 11 11:22:53 2007
@@ -50,6 +50,13 @@
   
   MacroInfo &getMacro() const { return Macro; }
 
+  /// getInstantiationLoc - Return a SourceLocation that specifies a macro
+  /// instantiation whose physical location is PhysLoc but the logical location
+  /// is InstantiationLoc.
+  static SourceLocation getInstantiationLoc(Preprocessor &PP,
+                                            SourceLocation PhysLoc,
+                                            SourceLocation InstantiationLoc);
+  
   /// Lex - Lex and return a token from this macro stream.
   void Lex(LexerToken &Tok);
 };





More information about the cfe-commits mailing list