浏览代码

[NFC] Extract method to SourceManager for traversing the macro "stack"

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
George Karpenkov 7 年之前
父节点
当前提交
791a735f8c
共有 4 个文件被更改,包括 13 次插入9 次删除
  1. 3 0
      include/clang/Basic/SourceManager.h
  2. 6 0
      lib/Basic/SourceManager.cpp
  3. 2 4
      lib/Edit/Commit.cpp
  4. 2 5
      lib/Sema/SemaChecking.cpp

+ 3 - 0
include/clang/Basic/SourceManager.h

@@ -1646,6 +1646,9 @@ public:
     return getImmediateExpansionRange(Loc).first;
     return getImmediateExpansionRange(Loc).first;
   }
   }
 
 
+  /// \return Location of the top-level macro caller.
+  SourceLocation getTopMacroCallerLoc(SourceLocation Loc) const;
+
 private:
 private:
   friend class ASTReader;
   friend class ASTReader;
   friend class ASTWriter;
   friend class ASTWriter;

+ 6 - 0
lib/Basic/SourceManager.cpp

@@ -955,6 +955,12 @@ SourceManager::getImmediateExpansionRange(SourceLocation Loc) const {
   return Expansion.getExpansionLocRange();
   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
 /// getExpansionRange - Given a SourceLocation object, return the range of
 /// tokens covered by the expansion in the ultimate file.
 /// tokens covered by the expansion in the ultimate file.
 std::pair<SourceLocation,SourceLocation>
 std::pair<SourceLocation,SourceLocation>

+ 2 - 4
lib/Edit/Commit.cpp

@@ -225,8 +225,7 @@ bool Commit::canInsert(SourceLocation loc, FileOffset &offs) {
     isAtStartOfMacroExpansion(loc, &loc);
     isAtStartOfMacroExpansion(loc, &loc);
 
 
   const SourceManager &SM = SourceMgr;
   const SourceManager &SM = SourceMgr;
-  while (SM.isMacroArgExpansion(loc))
-    loc = SM.getImmediateSpellingLoc(loc);
+  loc = SM.getTopMacroCallerLoc(loc);
 
 
   if (loc.isMacroID())
   if (loc.isMacroID())
     if (!isAtStartOfMacroExpansion(loc, &loc))
     if (!isAtStartOfMacroExpansion(loc, &loc))
@@ -256,8 +255,7 @@ bool Commit::canInsertAfterToken(SourceLocation loc, FileOffset &offs,
     isAtEndOfMacroExpansion(loc, &loc);
     isAtEndOfMacroExpansion(loc, &loc);
 
 
   const SourceManager &SM = SourceMgr;
   const SourceManager &SM = SourceMgr;
-  while (SM.isMacroArgExpansion(loc))
-    loc = SM.getImmediateSpellingLoc(loc);
+  loc = SM.getTopMacroCallerLoc(loc);
 
 
   if (loc.isMacroID())
   if (loc.isMacroID())
     if (!isAtEndOfMacroExpansion(loc, &loc))
     if (!isAtEndOfMacroExpansion(loc, &loc))

+ 2 - 5
lib/Sema/SemaChecking.cpp

@@ -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.
   // 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
   // The new location is a better location than the complete location that was
   // passed in.
   // 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.
   // __null is usually wrapped in a macro.  Go up a macro if that is the case.
   if (NullKind == Expr::NPCK_GNUNull && Loc.isMacroID()) {
   if (NullKind == Expr::NPCK_GNUNull && Loc.isMacroID()) {