]> granicus.if.org Git - clang/commitdiff
[NFC] Extract method to SourceManager for traversing the macro "stack"
authorGeorge Karpenkov <ekarpenkov@apple.com>
Fri, 9 Feb 2018 23:30:07 +0000 (23:30 +0000)
committerGeorge Karpenkov <ekarpenkov@apple.com>
Fri, 9 Feb 2018 23:30:07 +0000 (23:30 +0000)
The code for going up the macro arg expansion is duplicated in many
places (and we need it for the analyzer as well, so I did not want to
duplicate it two more times).

This patch is an NFC, so the semantics should remain the same.

Differential Revision: https://reviews.llvm.org/D42458

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@324780 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/SourceManager.h
lib/Basic/SourceManager.cpp
lib/Edit/Commit.cpp
lib/Sema/SemaChecking.cpp

index fe5aedaf20ce827ab7551ea84e3d7dff7c2752ef..17601b82939d414f776541aa4b81984e539af948 100644 (file)
@@ -1646,6 +1646,9 @@ public:
     return getImmediateExpansionRange(Loc).first;
   }
 
+  /// \return Location of the top-level macro caller.
+  SourceLocation getTopMacroCallerLoc(SourceLocation Loc) const;
+
 private:
   friend class ASTReader;
   friend class ASTWriter;
index 0a51985614c80b699ff3b3b8850780382f136d5c..3bf70b6669314e8b3af91b301aa0019d6f3d7668 100644 (file)
@@ -955,6 +955,12 @@ SourceManager::getImmediateExpansionRange(SourceLocation Loc) const {
   return Expansion.getExpansionLocRange();
 }
 
+SourceLocation SourceManager::getTopMacroCallerLoc(SourceLocation Loc) const {
+  while (isMacroArgExpansion(Loc))
+    Loc = getImmediateSpellingLoc(Loc);
+  return Loc;
+}
+
 /// getExpansionRange - Given a SourceLocation object, return the range of
 /// tokens covered by the expansion in the ultimate file.
 std::pair<SourceLocation,SourceLocation>
index cb7a784a41af4fc056c5cdfdb726307cc07d9e99..0cc152bf1e75b7b057a1adafe66379486eeff58c 100644 (file)
@@ -225,8 +225,7 @@ bool Commit::canInsert(SourceLocation loc, FileOffset &offs) {
     isAtStartOfMacroExpansion(loc, &loc);
 
   const SourceManager &SM = SourceMgr;
-  while (SM.isMacroArgExpansion(loc))
-    loc = SM.getImmediateSpellingLoc(loc);
+  loc = SM.getTopMacroCallerLoc(loc);
 
   if (loc.isMacroID())
     if (!isAtStartOfMacroExpansion(loc, &loc))
@@ -256,8 +255,7 @@ bool Commit::canInsertAfterToken(SourceLocation loc, FileOffset &offs,
     isAtEndOfMacroExpansion(loc, &loc);
 
   const SourceManager &SM = SourceMgr;
-  while (SM.isMacroArgExpansion(loc))
-    loc = SM.getImmediateSpellingLoc(loc);
+  loc = SM.getTopMacroCallerLoc(loc);
 
   if (loc.isMacroID())
     if (!isAtEndOfMacroExpansion(loc, &loc))
index 8f6eef5984355ea340940fab6b88cd8ddd028979..8f6fa10dd8fce2297fc0c1ddddd521e218ba641d 100644 (file)
@@ -9357,11 +9357,8 @@ static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T,
   // Venture through the macro stacks to get to the source of macro arguments.
   // The new location is a better location than the complete location that was
   // passed in.
-  while (S.SourceMgr.isMacroArgExpansion(Loc))
-    Loc = S.SourceMgr.getImmediateMacroCallerLoc(Loc);
-
-  while (S.SourceMgr.isMacroArgExpansion(CC))
-    CC = S.SourceMgr.getImmediateMacroCallerLoc(CC);
+  Loc = S.SourceMgr.getTopMacroCallerLoc(Loc);
+  CC = S.SourceMgr.getTopMacroCallerLoc(CC);
 
   // __null is usually wrapped in a macro.  Go up a macro if that is the case.
   if (NullKind == Expr::NPCK_GNUNull && Loc.isMacroID()) {