[clang-tools-extra] r288498 - [clang-move] some tweaks.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 2 04:39:39 PST 2016


Author: hokein
Date: Fri Dec  2 06:39:39 2016
New Revision: 288498

URL: http://llvm.org/viewvc/llvm-project?rev=288498&view=rev
Log:
[clang-move] some tweaks.

* Don't save SourceManager for each declarations.
* Rename some out-dated methods.

No functionality change.

Modified:
    clang-tools-extra/trunk/clang-move/ClangMove.cpp
    clang-tools-extra/trunk/clang-move/ClangMove.h

Modified: clang-tools-extra/trunk/clang-move/ClangMove.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-move/ClangMove.cpp?rev=288498&r1=288497&r2=288498&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-move/ClangMove.cpp (original)
+++ clang-tools-extra/trunk/clang-move/ClangMove.cpp Fri Dec  2 06:39:39 2016
@@ -147,10 +147,9 @@ public:
     const clang::NamedDecl *D = FD;
     if (const auto *FTD = FD->getDescribedFunctionTemplate())
       D = FTD;
-    MoveTool->getMovedDecls().emplace_back(D,
-                                           &Result.Context->getSourceManager());
+    MoveTool->getMovedDecls().push_back(D);
     MoveTool->getUnremovedDeclsInOldHeader().erase(D);
-    MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back());
+    MoveTool->addRemovedDecl(D);
   }
 
 private:
@@ -180,8 +179,8 @@ private:
     // Skip inline class methods. isInline() ast matcher doesn't ignore this
     // case.
     if (!CMD->isInlined()) {
-      MoveTool->getMovedDecls().emplace_back(CMD, SM);
-      MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back());
+      MoveTool->getMovedDecls().push_back(CMD);
+      MoveTool->addRemovedDecl(CMD);
       // Get template class method from its method declaration as
       // UnremovedDecls stores template class method.
       if (const auto *FTD = CMD->getDescribedFunctionTemplate())
@@ -193,8 +192,8 @@ private:
 
   void MatchClassStaticVariable(const clang::NamedDecl *VD,
                                 clang::SourceManager* SM) {
-    MoveTool->getMovedDecls().emplace_back(VD, SM);
-    MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back());
+    MoveTool->getMovedDecls().push_back(VD);
+    MoveTool->addRemovedDecl(VD);
     MoveTool->getUnremovedDeclsInOldHeader().erase(VD);
   }
 
@@ -203,12 +202,12 @@ private:
     // Get class template from its class declaration as UnremovedDecls stores
     // class template.
     if (const auto *TC = CD->getDescribedClassTemplate())
-      MoveTool->getMovedDecls().emplace_back(TC, SM);
+      MoveTool->getMovedDecls().push_back(TC);
     else
-      MoveTool->getMovedDecls().emplace_back(CD, SM);
+      MoveTool->getMovedDecls().push_back(CD);
     MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back());
     MoveTool->getUnremovedDeclsInOldHeader().erase(
-        MoveTool->getMovedDecls().back().Decl);
+        MoveTool->getMovedDecls().back());
   }
 
   ClangMoveTool *MoveTool;
@@ -217,18 +216,19 @@ private:
 // Expand to get the end location of the line where the EndLoc of the given
 // Decl.
 SourceLocation
-getLocForEndOfDecl(const clang::Decl *D, const SourceManager *SM,
+getLocForEndOfDecl(const clang::Decl *D,
                    const LangOptions &LangOpts = clang::LangOptions()) {
-  std::pair<FileID, unsigned> LocInfo = SM->getDecomposedLoc(D->getLocEnd());
+  const auto &SM = D->getASTContext().getSourceManager();
+  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(D->getLocEnd());
   // Try to load the file buffer.
   bool InvalidTemp = false;
-  llvm::StringRef File = SM->getBufferData(LocInfo.first, &InvalidTemp);
+  llvm::StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp);
   if (InvalidTemp)
     return SourceLocation();
 
   const char *TokBegin = File.data() + LocInfo.second;
   // Lex from the start of the given location.
-  Lexer Lex(SM->getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(),
+  Lexer Lex(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(),
             TokBegin, File.end());
 
   llvm::SmallVector<char, 16> Line;
@@ -239,40 +239,42 @@ getLocForEndOfDecl(const clang::Decl *D,
   // If we already reach EOF, just return the EOF SourceLocation;
   // otherwise, move 1 offset ahead to include the trailing newline character
   // '\n'.
-  return SM->getLocForEndOfFile(LocInfo.first) == EndLoc
+  return SM.getLocForEndOfFile(LocInfo.first) == EndLoc
              ? EndLoc
              : EndLoc.getLocWithOffset(1);
 }
 
 // Get full range of a Decl including the comments associated with it.
 clang::CharSourceRange
-GetFullRange(const clang::SourceManager *SM, const clang::Decl *D,
+getFullRange(const clang::Decl *D,
              const clang::LangOptions &options = clang::LangOptions()) {
-  clang::SourceRange Full(SM->getExpansionLoc(D->getLocStart()),
-                          getLocForEndOfDecl(D, SM));
+  const auto &SM = D->getASTContext().getSourceManager();
+  clang::SourceRange Full(SM.getExpansionLoc(D->getLocStart()),
+                          getLocForEndOfDecl(D));
   // Expand to comments that are associated with the Decl.
   if (const auto *Comment = D->getASTContext().getRawCommentForDeclNoCache(D)) {
-    if (SM->isBeforeInTranslationUnit(Full.getEnd(), Comment->getLocEnd()))
+    if (SM.isBeforeInTranslationUnit(Full.getEnd(), Comment->getLocEnd()))
       Full.setEnd(Comment->getLocEnd());
     // FIXME: Don't delete a preceding comment, if there are no other entities
     // it could refer to.
-    if (SM->isBeforeInTranslationUnit(Comment->getLocStart(), Full.getBegin()))
+    if (SM.isBeforeInTranslationUnit(Comment->getLocStart(), Full.getBegin()))
       Full.setBegin(Comment->getLocStart());
   }
 
   return clang::CharSourceRange::getCharRange(Full);
 }
 
-std::string getDeclarationSourceText(const clang::Decl *D,
-                                     const clang::SourceManager *SM) {
-  llvm::StringRef SourceText = clang::Lexer::getSourceText(
-      GetFullRange(SM, D), *SM, clang::LangOptions());
+std::string getDeclarationSourceText(const clang::Decl *D) {
+  const auto &SM = D->getASTContext().getSourceManager();
+  llvm::StringRef SourceText =
+      clang::Lexer::getSourceText(getFullRange(D), SM, clang::LangOptions());
   return SourceText.str();
 }
 
-bool isInHeaderFile(const clang::SourceManager &SM, const clang::Decl *D,
+bool isInHeaderFile(const clang::Decl *D,
                     llvm::StringRef OriginalRunningDirectory,
                     llvm::StringRef OldHeader) {
+  const auto &SM = D->getASTContext().getSourceManager();
   if (OldHeader.empty())
     return false;
   auto ExpansionLoc = SM.getExpansionLoc(D->getLocStart());
@@ -287,7 +289,7 @@ bool isInHeaderFile(const clang::SourceM
   return false;
 }
 
-std::vector<std::string> GetNamespaces(const clang::Decl *D) {
+std::vector<std::string> getNamespaces(const clang::Decl *D) {
   std::vector<std::string> Namespaces;
   for (const auto *Context = D->getDeclContext(); Context;
        Context = Context->getParent()) {
@@ -304,7 +306,7 @@ std::vector<std::string> GetNamespaces(c
 
 clang::tooling::Replacements
 createInsertedReplacements(const std::vector<std::string> &Includes,
-                           const std::vector<ClangMoveTool::MovedDecl> &Decls,
+                           const std::vector<const NamedDecl *> &Decls,
                            llvm::StringRef FileName, bool IsHeader = false,
                            StringRef OldHeaderInclude = "") {
   std::string NewCode;
@@ -332,9 +334,9 @@ createInsertedReplacements(const std::ve
   //
   // Record namespaces where the current position is in.
   std::vector<std::string> CurrentNamespaces;
-  for (const auto &MovedDecl : Decls) {
+  for (const auto *MovedDecl : Decls) {
     // The namespaces of the declaration being moved.
-    std::vector<std::string> DeclNamespaces = GetNamespaces(MovedDecl.Decl);
+    std::vector<std::string> DeclNamespaces = getNamespaces(MovedDecl);
     auto CurrentIt = CurrentNamespaces.begin();
     auto DeclIt = DeclNamespaces.begin();
     // Skip the common prefix.
@@ -378,7 +380,7 @@ createInsertedReplacements(const std::ve
     // FIXME: Don't add empty lines between using declarations.
     if (!IsInNewNamespace)
       NewCode += "\n";
-    NewCode += getDeclarationSourceText(MovedDecl.Decl, MovedDecl.SM);
+    NewCode += getDeclarationSourceText(MovedDecl);
     CurrentNamespaces = std::move(NextNamespaces);
   }
   std::reverse(CurrentNamespaces.begin(), CurrentNamespaces.end());
@@ -408,9 +410,9 @@ ClangMoveTool::ClangMoveTool(ClangMoveCo
     CCIncludes.push_back("#include \"" + Context->Spec.NewHeader + "\"\n");
 }
 
-void ClangMoveTool::addRemovedDecl(const MovedDecl &Decl) {
-  const auto &SM = *Decl.SM;
-  auto Loc = Decl.Decl->getLocation();
+void ClangMoveTool::addRemovedDecl(const NamedDecl *Decl) {
+  const auto &SM = Decl->getASTContext().getSourceManager();
+  auto Loc = Decl->getLocation();
   StringRef FilePath = SM.getFilename(Loc);
   FilePathToFileID[FilePath] = SM.getFileID(Loc);
   RemovedDecls.push_back(Decl);
@@ -536,19 +538,19 @@ void ClangMoveTool::run(const ast_matche
     // Skip all forward declarations which appear after moved class declaration.
     if (RemovedDecls.empty()) {
       if (const auto *DCT = FWD->getDescribedClassTemplate())
-        MovedDecls.emplace_back(DCT, &Result.Context->getSourceManager());
+        MovedDecls.push_back(DCT);
       else
-        MovedDecls.emplace_back(FWD, &Result.Context->getSourceManager());
+        MovedDecls.push_back(FWD);
     }
   } else if (const auto *ANS =
                  Result.Nodes.getNodeAs<clang::NamespaceDecl>("anonymous_ns")) {
-    MovedDecls.emplace_back(ANS, &Result.Context->getSourceManager());
+    MovedDecls.push_back(ANS);
   } else if (const auto *ND =
                  Result.Nodes.getNodeAs<clang::NamedDecl>("static_decls")) {
-    MovedDecls.emplace_back(ND, &Result.Context->getSourceManager());
+    MovedDecls.push_back(ND);
   } else if (const auto *UD =
                  Result.Nodes.getNodeAs<clang::NamedDecl>("using_decl")) {
-    MovedDecls.emplace_back(UD, &Result.Context->getSourceManager());
+    MovedDecls.push_back(UD);
   }
 }
 
@@ -586,11 +588,11 @@ void ClangMoveTool::addIncludes(llvm::St
   }
 }
 
-void ClangMoveTool::removeClassDefinitionInOldFiles() {
+void ClangMoveTool::removeDeclsInOldFiles() {
   if (RemovedDecls.empty()) return;
-  for (const auto &MovedDecl : RemovedDecls) {
-    const auto &SM = *MovedDecl.SM;
-    auto Range = GetFullRange(&SM, MovedDecl.Decl);
+  for (const auto *RemovedDecl : RemovedDecls) {
+    const auto &SM = RemovedDecl->getASTContext().getSourceManager();
+    auto Range = getFullRange(RemovedDecl);
     clang::tooling::Replacement RemoveReplacement(
         SM,
         clang::CharSourceRange::getCharRange(Range.getBegin(), Range.getEnd()),
@@ -600,14 +602,14 @@ void ClangMoveTool::removeClassDefinitio
     if (Err)
       llvm::errs() << llvm::toString(std::move(Err)) << "\n";
   }
-  const SourceManager* SM = RemovedDecls[0].SM;
+  const auto &SM = RemovedDecls[0]->getASTContext().getSourceManager();
 
   // Post process of cleanup around all the replacements.
   for (auto &FileAndReplacements : Context->FileToReplacements) {
     StringRef FilePath = FileAndReplacements.first;
     // Add #include of new header to old header.
     if (Context->Spec.OldDependOnNew &&
-        MakeAbsolutePath(*SM, FilePath) ==
+        MakeAbsolutePath(SM, FilePath) ==
             makeAbsolutePath(Context->Spec.OldHeader)) {
       // FIXME: Minimize the include path like include-fixer.
       std::string IncludeNewH =
@@ -622,7 +624,7 @@ void ClangMoveTool::removeClassDefinitio
     auto SI = FilePathToFileID.find(FilePath);
     // Ignore replacements for new.h/cc.
     if (SI == FilePathToFileID.end()) continue;
-    llvm::StringRef Code = SM->getBufferData(SI->second);
+    llvm::StringRef Code = SM.getBufferData(SI->second);
     format::FormatStyle Style =
         format::getStyle("file", FilePath, Context->FallbackStyle);
     auto CleanReplacements = format::cleanupAroundReplacements(
@@ -636,12 +638,11 @@ void ClangMoveTool::removeClassDefinitio
   }
 }
 
-void ClangMoveTool::moveClassDefinitionToNewFiles() {
-  std::vector<MovedDecl> NewHeaderDecls;
-  std::vector<MovedDecl> NewCCDecls;
-  for (const auto &MovedDecl : MovedDecls) {
-    if (isInHeaderFile(*MovedDecl.SM, MovedDecl.Decl,
-                       Context->OriginalRunningDirectory,
+void ClangMoveTool::moveDeclsToNewFiles() {
+  std::vector<const NamedDecl *> NewHeaderDecls;
+  std::vector<const NamedDecl *> NewCCDecls;
+  for (const auto *MovedDecl : MovedDecls) {
+    if (isInHeaderFile(MovedDecl, Context->OriginalRunningDirectory,
                        Context->Spec.OldHeader))
       NewHeaderDecls.push_back(MovedDecl);
     else
@@ -713,13 +714,13 @@ void ClangMoveTool::onEndOfTranslationUn
   if (RemovedDecls.empty())
     return;
   if (UnremovedDeclsInOldHeader.empty() && !Context->Spec.OldHeader.empty()) {
-    auto &SM = *RemovedDecls[0].SM;
+    auto &SM = RemovedDecls[0]->getASTContext().getSourceManager();
     moveAll(SM, Context->Spec.OldHeader, Context->Spec.NewHeader);
     moveAll(SM, Context->Spec.OldCC, Context->Spec.NewCC);
     return;
   }
-  removeClassDefinitionInOldFiles();
-  moveClassDefinitionToNewFiles();
+  removeDeclsInOldFiles();
+  moveDeclsToNewFiles();
 }
 
 } // namespace move

Modified: clang-tools-extra/trunk/clang-move/ClangMove.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-move/ClangMove.h?rev=288498&r1=288497&r2=288498&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-move/ClangMove.h (original)
+++ clang-tools-extra/trunk/clang-move/ClangMove.h Fri Dec  2 06:39:39 2016
@@ -98,17 +98,6 @@ struct ClangMoveContext {
 // old.h/cc will be moved, which means old.h/cc are empty.
 class ClangMoveTool : public ast_matchers::MatchFinder::MatchCallback {
 public:
-  // Information about the declaration being moved.
-  struct MovedDecl {
-    // FIXME: Replace Decl with SourceRange to get rid of calculating range for
-    // the Decl duplicately.
-    const clang::NamedDecl *Decl = nullptr;
-    clang::SourceManager *SM = nullptr;
-    MovedDecl() = default;
-    MovedDecl(const clang::NamedDecl *Decl, clang::SourceManager *SM)
-        : Decl(Decl), SM(SM) {}
-  };
-
   ClangMoveTool(ClangMoveContext *const Context,
                 DeclarationReporter *const Reporter);
 
@@ -134,12 +123,12 @@ public:
                    clang::CharSourceRange IncludeFilenameRange,
                    const SourceManager &SM);
 
-  std::vector<MovedDecl> &getMovedDecls() { return MovedDecls; }
+  std::vector<const NamedDecl *> &getMovedDecls() { return MovedDecls; }
 
   /// Add declarations being removed from old.h/cc. For each declarations, the
   /// method also records the mapping relationship between the corresponding
   /// FilePath and its FileID.
-  void addRemovedDecl(const MovedDecl &Decl);
+  void addRemovedDecl(const NamedDecl *Decl);
 
   llvm::SmallPtrSet<const NamedDecl *, 8> &getUnremovedDeclsInOldHeader() {
     return UnremovedDeclsInOldHeader;
@@ -150,8 +139,8 @@ private:
   // an absolute path. An empty Path will result in an empty string.
   std::string makeAbsolutePath(StringRef Path);
 
-  void removeClassDefinitionInOldFiles();
-  void moveClassDefinitionToNewFiles();
+  void removeDeclsInOldFiles();
+  void moveDeclsToNewFiles();
   void moveAll(SourceManager& SM, StringRef OldFile, StringRef NewFile);
 
   // Stores all MatchCallbacks created by this tool.
@@ -159,9 +148,9 @@ private:
       MatchCallbacks;
   // All declarations (the class decl being moved, forward decls) that need to
   // be moved/copy to the new files, saving in an AST-visited order.
-  std::vector<MovedDecl> MovedDecls;
+  std::vector<const NamedDecl *> MovedDecls;
   // The declarations that needs to be removed in old.cc/h.
-  std::vector<MovedDecl> RemovedDecls;
+  std::vector<const NamedDecl *> RemovedDecls;
   // The #includes in old_header.h.
   std::vector<std::string> HeaderIncludes;
   // The #includes in old_cc.cc.




More information about the cfe-commits mailing list