]> granicus.if.org Git - clang/commitdiff
Move SourceManager::isAt[Start/End]OfMacroInstantiation functions to the Lexer, since...
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 7 Jul 2011 21:54:45 +0000 (21:54 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 7 Jul 2011 21:54:45 +0000 (21:54 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@134644 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/SourceManager.h
include/clang/Lex/Lexer.h
include/clang/Lex/Preprocessor.h
lib/ARCMigrate/TransformActions.cpp
lib/ARCMigrate/Transforms.cpp
lib/Basic/SourceManager.cpp
lib/Lex/Lexer.cpp
lib/Parse/ParseExpr.cpp

index 33c66e7f7c057b3f8a01fc727779a6f1dccb8a05..bd8f5ef6ef8d8180c8528ee9ae3ed53a74754832 100644 (file)
@@ -832,16 +832,6 @@ public:
     return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
   }
 
-  /// \brief Returns true if the given MacroID location points at the first
-  /// token of the macro instantiation.
-  bool isAtStartOfMacroInstantiation(SourceLocation Loc,
-                                     const LangOptions &LangOpts) const;
-
-  /// \brief Returns true if the given MacroID location points at the last
-  /// token of the macro instantiation.
-  bool isAtEndOfMacroInstantiation(SourceLocation Loc,
-                                   const LangOptions &LangOpts) const;
-
   /// \brief Given a specific chunk of a FileID (FileID with offset+length),
   /// returns true if \arg Loc is inside that chunk and sets relative offset
   /// (offset of \arg Loc from beginning of chunk) to \arg relativeOffset.
index 7c3d863bd3d39cc1f8b34eb879d0a896ebc82ef6..937e4680341ede86db30e15a53cbbb6bb4676f71 100644 (file)
@@ -293,7 +293,19 @@ public:
   static SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset,
                                             const SourceManager &SM,
                                             const LangOptions &Features);
-    
+
+  /// \brief Returns true if the given MacroID location points at the first
+  /// token of the macro instantiation.
+  static bool isAtStartOfMacroInstantiation(SourceLocation loc,
+                                            const SourceManager &SM,
+                                            const LangOptions &LangOpts);
+
+  /// \brief Returns true if the given MacroID location points at the last
+  /// token of the macro instantiation.
+  static bool isAtEndOfMacroInstantiation(SourceLocation loc,
+                                          const SourceManager &SM,
+                                          const LangOptions &LangOpts);
+
   /// \brief Compute the preamble of the given file.
   ///
   /// The preamble of a file contains the initial comments, include directives,
index 56fc9d172757034e5d3d5c297e3339a3b30bdf66..e1ea37f241a8021f4b53245c6155e1b6f9133d15 100644 (file)
@@ -753,6 +753,18 @@ public:
     return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, Features);
   }
 
+  /// \brief Returns true if the given MacroID location points at the first
+  /// token of the macro instantiation.
+  bool isAtStartOfMacroInstantiation(SourceLocation loc) const {
+    return Lexer::isAtStartOfMacroInstantiation(loc, SourceMgr, Features);
+  }
+
+  /// \brief Returns true if the given MacroID location points at the last
+  /// token of the macro instantiation.
+  bool isAtEndOfMacroInstantiation(SourceLocation loc) const {
+    return Lexer::isAtEndOfMacroInstantiation(loc, SourceMgr, Features);
+  }
+
   /// DumpToken - Print the token to stderr, used for debugging.
   ///
   void DumpToken(const Token &Tok, bool DumpFlags = false) const;
index 2ae979896a5de4fd9b2bc3e27aea9c5c8def5f0e..3e49942788a708ff9d8bbff80770ccb23ecbe002 100644 (file)
@@ -388,7 +388,7 @@ bool TransformActionsImpl::canInsert(SourceLocation loc) {
 
   if (loc.isFileID())
     return true;
-  return SM.isAtStartOfMacroInstantiation(loc, Ctx.getLangOptions());
+  return PP.isAtStartOfMacroInstantiation(loc);
 }
 
 bool TransformActionsImpl::canInsertAfterToken(SourceLocation loc) {
@@ -401,7 +401,7 @@ bool TransformActionsImpl::canInsertAfterToken(SourceLocation loc) {
 
   if (loc.isFileID())
     return true;
-  return SM.isAtEndOfMacroInstantiation(loc, Ctx.getLangOptions());
+  return PP.isAtEndOfMacroInstantiation(loc);
 }
 
 bool TransformActionsImpl::canRemoveRange(SourceRange range) {
index 80c52d79405cbef748a03177f58188f9a7e5bdce..546829120c06880cffd95809cda5e1270094cfea 100644 (file)
@@ -37,7 +37,7 @@ SourceLocation trans::findLocationAfterSemi(SourceLocation loc,
                                             ASTContext &Ctx) {
   SourceManager &SM = Ctx.getSourceManager();
   if (loc.isMacroID()) {
-    if (!SM.isAtEndOfMacroInstantiation(loc, Ctx.getLangOptions()))
+    if (!Lexer::isAtEndOfMacroInstantiation(loc, SM, Ctx.getLangOptions()))
       return SourceLocation();
     loc = SM.getInstantiationRange(loc).second;
   }
index 137da0d87ada88ddbb662b91343b4a8289b88549..cd098b23aa53425659d41606ec7326c81c967833 100644 (file)
@@ -11,7 +11,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "clang/Lex/Lexer.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/SourceManagerInternals.h"
 #include "clang/Basic/Diagnostic.h"
@@ -1215,60 +1214,6 @@ PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
   return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
 }
 
-/// \brief Returns true if the given MacroID location points at the first
-/// token of the macro instantiation.
-bool SourceManager::isAtStartOfMacroInstantiation(SourceLocation loc,
-                                            const LangOptions &LangOpts) const {
-  assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc");
-
-  std::pair<FileID, unsigned> infoLoc = getDecomposedLoc(loc);
-  // FIXME: If the token comes from the macro token paste operator ('##')
-  // this function will always return false;
-  if (infoLoc.second > 0)
-    return false; // Does not point at the start of token.
-
-  SourceLocation instLoc = 
-      getSLocEntry(infoLoc.first).getInstantiation().getInstantiationLocStart();
-  if (instLoc.isFileID())
-    return true; // No other macro instantiations, this is the first.
-
-  return isAtStartOfMacroInstantiation(instLoc, LangOpts);
-}
-
-/// \brief Returns true if the given MacroID location points at the last
-/// token of the macro instantiation.
-bool SourceManager::isAtEndOfMacroInstantiation(SourceLocation loc,
-                                            const LangOptions &LangOpts) const {
-  assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc");
-
-  SourceLocation spellLoc = getSpellingLoc(loc);
-  unsigned tokLen = Lexer::MeasureTokenLength(spellLoc, *this, LangOpts);
-  if (tokLen == 0)
-    return false;
-
-  std::pair<FileID, unsigned> infoLoc = getDecomposedLoc(loc);
-  unsigned FID = infoLoc.first.ID;
-
-  unsigned NextOffset;
-  if (FID+1 == sloc_entry_size())
-    NextOffset = getNextOffset();
-  else
-    NextOffset = getSLocEntry(FID+1).getOffset();
-
-  // FIXME: If the token comes from the macro token paste operator ('##')
-  // or the stringify operator ('#') this function will always return false;
-  assert(loc.getOffset() + tokLen < NextOffset);
-  if (loc.getOffset() + tokLen < NextOffset-1)
-    return false; // Does not point to the last token.
-  
-  SourceLocation instLoc = 
-      getSLocEntry(infoLoc.first).getInstantiation().getInstantiationLocEnd();
-  if (instLoc.isFileID())
-    return true; // No other macro instantiations.
-
-  return isAtEndOfMacroInstantiation(instLoc, LangOpts);
-}
-
 //===----------------------------------------------------------------------===//
 // Other miscellaneous methods.
 //===----------------------------------------------------------------------===//
index 6d25d2b2cf52947e9bb9b0d9acc1605d8ac822eb..aabba0aa566c1e1c05b8c8b561b2d10426119be3 100644 (file)
@@ -683,7 +683,7 @@ SourceLocation Lexer::getLocForEndOfToken(SourceLocation Loc, unsigned Offset,
     return SourceLocation();
 
   if (Loc.isMacroID()) {
-    if (Offset > 0 || !SM.isAtEndOfMacroInstantiation(Loc, Features))
+    if (Offset > 0 || !isAtEndOfMacroInstantiation(Loc, SM, Features))
       return SourceLocation(); // Points inside the macro instantiation.
 
     // Continue and find the location just after the macro instantiation.
@@ -699,6 +699,57 @@ SourceLocation Lexer::getLocForEndOfToken(SourceLocation Loc, unsigned Offset,
   return Loc.getFileLocWithOffset(Len);
 }
 
+/// \brief Returns true if the given MacroID location points at the first
+/// token of the macro instantiation.
+bool Lexer::isAtStartOfMacroInstantiation(SourceLocation loc,
+                                          const SourceManager &SM,
+                                          const LangOptions &LangOpts) {
+  assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc");
+
+  std::pair<FileID, unsigned> infoLoc = SM.getDecomposedLoc(loc);
+  // FIXME: If the token comes from the macro token paste operator ('##')
+  // this function will always return false;
+  if (infoLoc.second > 0)
+    return false; // Does not point at the start of token.
+
+  SourceLocation instLoc = 
+      SM.getSLocEntry(infoLoc.first).getInstantiation().getInstantiationLocStart();
+  if (instLoc.isFileID())
+    return true; // No other macro instantiations, this is the first.
+
+  return isAtStartOfMacroInstantiation(instLoc, SM, LangOpts);
+}
+
+/// \brief Returns true if the given MacroID location points at the last
+/// token of the macro instantiation.
+bool Lexer::isAtEndOfMacroInstantiation(SourceLocation loc,
+                                        const SourceManager &SM,
+                                        const LangOptions &LangOpts) {
+  assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc");
+
+  SourceLocation spellLoc = SM.getSpellingLoc(loc);
+  unsigned tokLen = MeasureTokenLength(spellLoc, SM, LangOpts);
+  if (tokLen == 0)
+    return false;
+
+  FileID FID = SM.getFileID(loc);
+  SourceLocation afterLoc = loc.getFileLocWithOffset(tokLen+1);
+  if (!SM.isBeforeInSourceLocationOffset(afterLoc, SM.getNextOffset()))
+    return true; // We got past the last FileID, this points to the last token.
+
+  // FIXME: If the token comes from the macro token paste operator ('##')
+  // or the stringify operator ('#') this function will always return false;
+  if (FID == SM.getFileID(afterLoc))
+    return false; // Still in the same FileID, does not point to the last token.
+  
+  SourceLocation instLoc = 
+    SM.getSLocEntry(FID).getInstantiation().getInstantiationLocEnd();
+  if (instLoc.isFileID())
+    return true; // No other macro instantiations.
+
+  return isAtEndOfMacroInstantiation(instLoc, SM, LangOpts);
+}
+
 //===----------------------------------------------------------------------===//
 // Character information.
 //===----------------------------------------------------------------------===//
index f8652e0074758dd0c20cc640401f389e18f18c00..7df43d78749623da4a1acc9b6031198cc2dd09e0 100644 (file)
@@ -310,8 +310,7 @@ Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
         SourceLocation FILoc = Tok.getLocation();
         const char *FIText = ": ";
         const SourceManager &SM = PP.getSourceManager();
-        if (FILoc.isFileID() ||
-            SM.isAtStartOfMacroInstantiation(FILoc, getLang())) {
+        if (FILoc.isFileID() || PP.isAtStartOfMacroInstantiation(FILoc)) {
           FILoc = SM.getInstantiationLoc(FILoc);
           bool IsInvalid = false;
           const char *SourcePtr =