clang-tools  7.0.0
ClangMove.cpp
Go to the documentation of this file.
1 //===-- ClangMove.cpp - Implement ClangMove functationalities ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "ClangMove.h"
11 #include "HelperDeclRefGraph.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
13 #include "clang/Basic/SourceManager.h"
14 #include "clang/Format/Format.h"
15 #include "clang/Frontend/CompilerInstance.h"
16 #include "clang/Lex/Lexer.h"
17 #include "clang/Lex/Preprocessor.h"
18 #include "clang/Rewrite/Core/Rewriter.h"
19 #include "clang/Tooling/Core/Replacement.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Path.h"
22 
23 #define DEBUG_TYPE "clang-move"
24 
25 using namespace clang::ast_matchers;
26 
27 namespace clang {
28 namespace move {
29 namespace {
30 
31 // FIXME: Move to ASTMatchers.
32 AST_MATCHER(VarDecl, isStaticDataMember) { return Node.isStaticDataMember(); }
33 
34 AST_MATCHER(NamedDecl, notInMacro) { return !Node.getLocation().isMacroID(); }
35 
36 AST_MATCHER_P(Decl, hasOutermostEnclosingClass,
37  ast_matchers::internal::Matcher<Decl>, InnerMatcher) {
38  const auto *Context = Node.getDeclContext();
39  if (!Context)
40  return false;
41  while (const auto *NextContext = Context->getParent()) {
42  if (isa<NamespaceDecl>(NextContext) ||
43  isa<TranslationUnitDecl>(NextContext))
44  break;
45  Context = NextContext;
46  }
47  return InnerMatcher.matches(*Decl::castFromDeclContext(Context), Finder,
48  Builder);
49 }
50 
51 AST_MATCHER_P(CXXMethodDecl, ofOutermostEnclosingClass,
52  ast_matchers::internal::Matcher<CXXRecordDecl>, InnerMatcher) {
53  const CXXRecordDecl *Parent = Node.getParent();
54  if (!Parent)
55  return false;
56  while (const auto *NextParent =
57  dyn_cast<CXXRecordDecl>(Parent->getParent())) {
58  Parent = NextParent;
59  }
60 
61  return InnerMatcher.matches(*Parent, Finder, Builder);
62 }
63 
64 std::string CleanPath(StringRef PathRef) {
65  llvm::SmallString<128> Path(PathRef);
66  llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
67  // FIXME: figure out why this is necessary.
68  llvm::sys::path::native(Path);
69  return Path.str();
70 }
71 
72 // Make the Path absolute using the CurrentDir if the Path is not an absolute
73 // path. An empty Path will result in an empty string.
74 std::string MakeAbsolutePath(StringRef CurrentDir, StringRef Path) {
75  if (Path.empty())
76  return "";
77  llvm::SmallString<128> InitialDirectory(CurrentDir);
78  llvm::SmallString<128> AbsolutePath(Path);
79  if (std::error_code EC =
80  llvm::sys::fs::make_absolute(InitialDirectory, AbsolutePath))
81  llvm::errs() << "Warning: could not make absolute file: '" << EC.message()
82  << '\n';
83  return CleanPath(std::move(AbsolutePath));
84 }
85 
86 // Make the Path absolute using the current working directory of the given
87 // SourceManager if the Path is not an absolute path.
88 //
89 // The Path can be a path relative to the build directory, or retrieved from
90 // the SourceManager.
91 std::string MakeAbsolutePath(const SourceManager &SM, StringRef Path) {
92  llvm::SmallString<128> AbsolutePath(Path);
93  if (std::error_code EC =
94  SM.getFileManager().getVirtualFileSystem()->makeAbsolute(
95  AbsolutePath))
96  llvm::errs() << "Warning: could not make absolute file: '" << EC.message()
97  << '\n';
98  // Handle symbolic link path cases.
99  // We are trying to get the real file path of the symlink.
100  const DirectoryEntry *Dir = SM.getFileManager().getDirectory(
101  llvm::sys::path::parent_path(AbsolutePath.str()));
102  if (Dir) {
103  StringRef DirName = SM.getFileManager().getCanonicalName(Dir);
104  // FIXME: getCanonicalName might fail to get real path on VFS.
105  if (llvm::sys::path::is_absolute(DirName)) {
106  SmallString<128> AbsoluteFilename;
107  llvm::sys::path::append(AbsoluteFilename, DirName,
108  llvm::sys::path::filename(AbsolutePath.str()));
109  return CleanPath(AbsoluteFilename);
110  }
111  }
112  return CleanPath(AbsolutePath);
113 }
114 
115 // Matches AST nodes that are expanded within the given AbsoluteFilePath.
116 AST_POLYMORPHIC_MATCHER_P(isExpansionInFile,
117  AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc),
118  std::string, AbsoluteFilePath) {
119  auto &SourceManager = Finder->getASTContext().getSourceManager();
120  auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getLocStart());
121  if (ExpansionLoc.isInvalid())
122  return false;
123  auto FileEntry =
124  SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc));
125  if (!FileEntry)
126  return false;
127  return MakeAbsolutePath(SourceManager, FileEntry->getName()) ==
128  AbsoluteFilePath;
129 }
130 
131 class FindAllIncludes : public clang::PPCallbacks {
132 public:
133  explicit FindAllIncludes(SourceManager *SM, ClangMoveTool *const MoveTool)
134  : SM(*SM), MoveTool(MoveTool) {}
135 
136  void InclusionDirective(clang::SourceLocation HashLoc,
137  const clang::Token & /*IncludeTok*/,
138  StringRef FileName, bool IsAngled,
139  clang::CharSourceRange FilenameRange,
140  const clang::FileEntry * /*File*/,
141  StringRef SearchPath, StringRef /*RelativePath*/,
142  const clang::Module * /*Imported*/,
143  SrcMgr::CharacteristicKind /*FileType*/) override {
144  if (const auto *FileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc)))
145  MoveTool->addIncludes(FileName, IsAngled, SearchPath,
146  FileEntry->getName(), FilenameRange, SM);
147  }
148 
149 private:
150  const SourceManager &SM;
151  ClangMoveTool *const MoveTool;
152 };
153 
154 /// Add a declatration being moved to new.h/cc. Note that the declaration will
155 /// also be deleted in old.h/cc.
156 void MoveDeclFromOldFileToNewFile(ClangMoveTool *MoveTool, const NamedDecl *D) {
157  MoveTool->getMovedDecls().push_back(D);
158  MoveTool->addRemovedDecl(D);
159  MoveTool->getUnremovedDeclsInOldHeader().erase(D);
160 }
161 
162 class FunctionDeclarationMatch : public MatchFinder::MatchCallback {
163 public:
164  explicit FunctionDeclarationMatch(ClangMoveTool *MoveTool)
165  : MoveTool(MoveTool) {}
166 
167  void run(const MatchFinder::MatchResult &Result) override {
168  const auto *FD = Result.Nodes.getNodeAs<clang::FunctionDecl>("function");
169  assert(FD);
170  const clang::NamedDecl *D = FD;
171  if (const auto *FTD = FD->getDescribedFunctionTemplate())
172  D = FTD;
173  MoveDeclFromOldFileToNewFile(MoveTool, D);
174  }
175 
176 private:
177  ClangMoveTool *MoveTool;
178 };
179 
180 class VarDeclarationMatch : public MatchFinder::MatchCallback {
181 public:
182  explicit VarDeclarationMatch(ClangMoveTool *MoveTool)
183  : MoveTool(MoveTool) {}
184 
185  void run(const MatchFinder::MatchResult &Result) override {
186  const auto *VD = Result.Nodes.getNodeAs<clang::VarDecl>("var");
187  assert(VD);
188  MoveDeclFromOldFileToNewFile(MoveTool, VD);
189  }
190 
191 private:
192  ClangMoveTool *MoveTool;
193 };
194 
195 class TypeAliasMatch : public MatchFinder::MatchCallback {
196 public:
197  explicit TypeAliasMatch(ClangMoveTool *MoveTool)
198  : MoveTool(MoveTool) {}
199 
200  void run(const MatchFinder::MatchResult &Result) override {
201  if (const auto *TD = Result.Nodes.getNodeAs<clang::TypedefDecl>("typedef"))
202  MoveDeclFromOldFileToNewFile(MoveTool, TD);
203  else if (const auto *TAD =
204  Result.Nodes.getNodeAs<clang::TypeAliasDecl>("type_alias")) {
205  const NamedDecl * D = TAD;
206  if (const auto * TD = TAD->getDescribedAliasTemplate())
207  D = TD;
208  MoveDeclFromOldFileToNewFile(MoveTool, D);
209  }
210  }
211 
212 private:
213  ClangMoveTool *MoveTool;
214 };
215 
216 class EnumDeclarationMatch : public MatchFinder::MatchCallback {
217 public:
218  explicit EnumDeclarationMatch(ClangMoveTool *MoveTool)
219  : MoveTool(MoveTool) {}
220 
221  void run(const MatchFinder::MatchResult &Result) override {
222  const auto *ED = Result.Nodes.getNodeAs<clang::EnumDecl>("enum");
223  assert(ED);
224  MoveDeclFromOldFileToNewFile(MoveTool, ED);
225  }
226 
227 private:
228  ClangMoveTool *MoveTool;
229 };
230 
231 class ClassDeclarationMatch : public MatchFinder::MatchCallback {
232 public:
233  explicit ClassDeclarationMatch(ClangMoveTool *MoveTool)
234  : MoveTool(MoveTool) {}
235  void run(const MatchFinder::MatchResult &Result) override {
236  clang::SourceManager* SM = &Result.Context->getSourceManager();
237  if (const auto *CMD =
238  Result.Nodes.getNodeAs<clang::CXXMethodDecl>("class_method"))
239  MatchClassMethod(CMD, SM);
240  else if (const auto *VD = Result.Nodes.getNodeAs<clang::VarDecl>(
241  "class_static_var_decl"))
242  MatchClassStaticVariable(VD, SM);
243  else if (const auto *CD = Result.Nodes.getNodeAs<clang::CXXRecordDecl>(
244  "moved_class"))
245  MatchClassDeclaration(CD, SM);
246  }
247 
248 private:
249  void MatchClassMethod(const clang::CXXMethodDecl* CMD,
250  clang::SourceManager* SM) {
251  // Skip inline class methods. isInline() ast matcher doesn't ignore this
252  // case.
253  if (!CMD->isInlined()) {
254  MoveTool->getMovedDecls().push_back(CMD);
255  MoveTool->addRemovedDecl(CMD);
256  // Get template class method from its method declaration as
257  // UnremovedDecls stores template class method.
258  if (const auto *FTD = CMD->getDescribedFunctionTemplate())
259  MoveTool->getUnremovedDeclsInOldHeader().erase(FTD);
260  else
261  MoveTool->getUnremovedDeclsInOldHeader().erase(CMD);
262  }
263  }
264 
265  void MatchClassStaticVariable(const clang::NamedDecl *VD,
266  clang::SourceManager* SM) {
267  MoveDeclFromOldFileToNewFile(MoveTool, VD);
268  }
269 
270  void MatchClassDeclaration(const clang::CXXRecordDecl *CD,
271  clang::SourceManager* SM) {
272  // Get class template from its class declaration as UnremovedDecls stores
273  // class template.
274  if (const auto *TC = CD->getDescribedClassTemplate())
275  MoveTool->getMovedDecls().push_back(TC);
276  else
277  MoveTool->getMovedDecls().push_back(CD);
278  MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back());
279  MoveTool->getUnremovedDeclsInOldHeader().erase(
280  MoveTool->getMovedDecls().back());
281  }
282 
283  ClangMoveTool *MoveTool;
284 };
285 
286 // Expand to get the end location of the line where the EndLoc of the given
287 // Decl.
288 SourceLocation
289 getLocForEndOfDecl(const clang::Decl *D,
290  const LangOptions &LangOpts = clang::LangOptions()) {
291  const auto &SM = D->getASTContext().getSourceManager();
292  // If the expansion range is a character range, this is the location of
293  // the first character past the end. Otherwise it's the location of the
294  // first character in the final token in the range.
295  auto EndExpansionLoc = SM.getExpansionRange(D->getLocEnd()).getEnd();
296  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(EndExpansionLoc);
297  // Try to load the file buffer.
298  bool InvalidTemp = false;
299  llvm::StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp);
300  if (InvalidTemp)
301  return SourceLocation();
302 
303  const char *TokBegin = File.data() + LocInfo.second;
304  // Lex from the start of the given location.
305  Lexer Lex(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(),
306  TokBegin, File.end());
307 
308  llvm::SmallVector<char, 16> Line;
309  // FIXME: this is a bit hacky to get ReadToEndOfLine work.
310  Lex.setParsingPreprocessorDirective(true);
311  Lex.ReadToEndOfLine(&Line);
312  SourceLocation EndLoc = EndExpansionLoc.getLocWithOffset(Line.size());
313  // If we already reach EOF, just return the EOF SourceLocation;
314  // otherwise, move 1 offset ahead to include the trailing newline character
315  // '\n'.
316  return SM.getLocForEndOfFile(LocInfo.first) == EndLoc
317  ? EndLoc
318  : EndLoc.getLocWithOffset(1);
319 }
320 
321 // Get full range of a Decl including the comments associated with it.
322 clang::CharSourceRange
323 getFullRange(const clang::Decl *D,
324  const clang::LangOptions &options = clang::LangOptions()) {
325  const auto &SM = D->getASTContext().getSourceManager();
326  clang::SourceRange Full(SM.getExpansionLoc(D->getLocStart()),
327  getLocForEndOfDecl(D));
328  // Expand to comments that are associated with the Decl.
329  if (const auto *Comment = D->getASTContext().getRawCommentForDeclNoCache(D)) {
330  if (SM.isBeforeInTranslationUnit(Full.getEnd(), Comment->getLocEnd()))
331  Full.setEnd(Comment->getLocEnd());
332  // FIXME: Don't delete a preceding comment, if there are no other entities
333  // it could refer to.
334  if (SM.isBeforeInTranslationUnit(Comment->getLocStart(), Full.getBegin()))
335  Full.setBegin(Comment->getLocStart());
336  }
337 
338  return clang::CharSourceRange::getCharRange(Full);
339 }
340 
341 std::string getDeclarationSourceText(const clang::Decl *D) {
342  const auto &SM = D->getASTContext().getSourceManager();
343  llvm::StringRef SourceText =
344  clang::Lexer::getSourceText(getFullRange(D), SM, clang::LangOptions());
345  return SourceText.str();
346 }
347 
348 bool isInHeaderFile(const clang::Decl *D,
349  llvm::StringRef OriginalRunningDirectory,
350  llvm::StringRef OldHeader) {
351  const auto &SM = D->getASTContext().getSourceManager();
352  if (OldHeader.empty())
353  return false;
354  auto ExpansionLoc = SM.getExpansionLoc(D->getLocStart());
355  if (ExpansionLoc.isInvalid())
356  return false;
357 
358  if (const auto *FE = SM.getFileEntryForID(SM.getFileID(ExpansionLoc))) {
359  return MakeAbsolutePath(SM, FE->getName()) ==
360  MakeAbsolutePath(OriginalRunningDirectory, OldHeader);
361  }
362 
363  return false;
364 }
365 
366 std::vector<std::string> getNamespaces(const clang::Decl *D) {
367  std::vector<std::string> Namespaces;
368  for (const auto *Context = D->getDeclContext(); Context;
369  Context = Context->getParent()) {
370  if (llvm::isa<clang::TranslationUnitDecl>(Context) ||
371  llvm::isa<clang::LinkageSpecDecl>(Context))
372  break;
373 
374  if (const auto *ND = llvm::dyn_cast<clang::NamespaceDecl>(Context))
375  Namespaces.push_back(ND->getName().str());
376  }
377  std::reverse(Namespaces.begin(), Namespaces.end());
378  return Namespaces;
379 }
380 
381 clang::tooling::Replacements
382 createInsertedReplacements(const std::vector<std::string> &Includes,
383  const std::vector<const NamedDecl *> &Decls,
384  llvm::StringRef FileName, bool IsHeader = false,
385  StringRef OldHeaderInclude = "") {
386  std::string NewCode;
387  std::string GuardName(FileName);
388  if (IsHeader) {
389  for (size_t i = 0; i < GuardName.size(); ++i) {
390  if (!isAlphanumeric(GuardName[i]))
391  GuardName[i] = '_';
392  }
393  GuardName = StringRef(GuardName).upper();
394  NewCode += "#ifndef " + GuardName + "\n";
395  NewCode += "#define " + GuardName + "\n\n";
396  }
397 
398  NewCode += OldHeaderInclude;
399  // Add #Includes.
400  for (const auto &Include : Includes)
401  NewCode += Include;
402 
403  if (!Includes.empty())
404  NewCode += "\n";
405 
406  // Add moved class definition and its related declarations. All declarations
407  // in same namespace are grouped together.
408  //
409  // Record namespaces where the current position is in.
410  std::vector<std::string> CurrentNamespaces;
411  for (const auto *MovedDecl : Decls) {
412  // The namespaces of the declaration being moved.
413  std::vector<std::string> DeclNamespaces = getNamespaces(MovedDecl);
414  auto CurrentIt = CurrentNamespaces.begin();
415  auto DeclIt = DeclNamespaces.begin();
416  // Skip the common prefix.
417  while (CurrentIt != CurrentNamespaces.end() &&
418  DeclIt != DeclNamespaces.end()) {
419  if (*CurrentIt != *DeclIt)
420  break;
421  ++CurrentIt;
422  ++DeclIt;
423  }
424  // Calculate the new namespaces after adding MovedDecl in CurrentNamespace,
425  // which is used for next iteration of this loop.
426  std::vector<std::string> NextNamespaces(CurrentNamespaces.begin(),
427  CurrentIt);
428  NextNamespaces.insert(NextNamespaces.end(), DeclIt, DeclNamespaces.end());
429 
430 
431  // End with CurrentNamespace.
432  bool HasEndCurrentNamespace = false;
433  auto RemainingSize = CurrentNamespaces.end() - CurrentIt;
434  for (auto It = CurrentNamespaces.rbegin(); RemainingSize > 0;
435  --RemainingSize, ++It) {
436  assert(It < CurrentNamespaces.rend());
437  NewCode += "} // namespace " + *It + "\n";
438  HasEndCurrentNamespace = true;
439  }
440  // Add trailing '\n' after the nested namespace definition.
441  if (HasEndCurrentNamespace)
442  NewCode += "\n";
443 
444  // If the moved declaration is not in CurrentNamespace, add extra namespace
445  // definitions.
446  bool IsInNewNamespace = false;
447  while (DeclIt != DeclNamespaces.end()) {
448  NewCode += "namespace " + *DeclIt + " {\n";
449  IsInNewNamespace = true;
450  ++DeclIt;
451  }
452  // If the moved declaration is in same namespace CurrentNamespace, add
453  // a preceeding `\n' before the moved declaration.
454  // FIXME: Don't add empty lines between using declarations.
455  if (!IsInNewNamespace)
456  NewCode += "\n";
457  NewCode += getDeclarationSourceText(MovedDecl);
458  CurrentNamespaces = std::move(NextNamespaces);
459  }
460  std::reverse(CurrentNamespaces.begin(), CurrentNamespaces.end());
461  for (const auto &NS : CurrentNamespaces)
462  NewCode += "} // namespace " + NS + "\n";
463 
464  if (IsHeader)
465  NewCode += "\n#endif // " + GuardName + "\n";
466  return clang::tooling::Replacements(
467  clang::tooling::Replacement(FileName, 0, 0, NewCode));
468 }
469 
470 // Return a set of all decls which are used/referenced by the given Decls.
471 // Specically, given a class member declaration, this method will return all
472 // decls which are used by the whole class.
473 llvm::DenseSet<const Decl *>
474 getUsedDecls(const HelperDeclRefGraph *RG,
475  const std::vector<const NamedDecl *> &Decls) {
476  assert(RG);
477  llvm::DenseSet<const CallGraphNode *> Nodes;
478  for (const auto *D : Decls) {
479  auto Result = RG->getReachableNodes(
480  HelperDeclRGBuilder::getOutmostClassOrFunDecl(D));
481  Nodes.insert(Result.begin(), Result.end());
482  }
483  llvm::DenseSet<const Decl *> Results;
484  for (const auto *Node : Nodes)
485  Results.insert(Node->getDecl());
486  return Results;
487 }
488 
489 } // namespace
490 
491 std::unique_ptr<clang::ASTConsumer>
492 ClangMoveAction::CreateASTConsumer(clang::CompilerInstance &Compiler,
493  StringRef /*InFile*/) {
494  Compiler.getPreprocessor().addPPCallbacks(llvm::make_unique<FindAllIncludes>(
495  &Compiler.getSourceManager(), &MoveTool));
496  return MatchFinder.newASTConsumer();
497 }
498 
499 ClangMoveTool::ClangMoveTool(ClangMoveContext *const Context,
500  DeclarationReporter *const Reporter)
501  : Context(Context), Reporter(Reporter) {
502  if (!Context->Spec.NewHeader.empty())
503  CCIncludes.push_back("#include \"" + Context->Spec.NewHeader + "\"\n");
504 }
505 
506 void ClangMoveTool::addRemovedDecl(const NamedDecl *Decl) {
507  const auto &SM = Decl->getASTContext().getSourceManager();
508  auto Loc = Decl->getLocation();
509  StringRef FilePath = SM.getFilename(Loc);
510  FilePathToFileID[FilePath] = SM.getFileID(Loc);
511  RemovedDecls.push_back(Decl);
512 }
513 
514 void ClangMoveTool::registerMatchers(ast_matchers::MatchFinder *Finder) {
515  auto InOldHeader =
516  isExpansionInFile(makeAbsolutePath(Context->Spec.OldHeader));
517  auto InOldCC = isExpansionInFile(makeAbsolutePath(Context->Spec.OldCC));
518  auto InOldFiles = anyOf(InOldHeader, InOldCC);
519  auto classTemplateForwardDecls =
520  classTemplateDecl(unless(has(cxxRecordDecl(isDefinition()))));
521  auto ForwardClassDecls = namedDecl(
522  anyOf(cxxRecordDecl(unless(anyOf(isImplicit(), isDefinition()))),
523  classTemplateForwardDecls));
524  auto TopLevelDecl =
525  hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl()));
526 
527  //============================================================================
528  // Matchers for old header
529  //============================================================================
530  // Match all top-level named declarations (e.g. function, variable, enum) in
531  // old header, exclude forward class declarations and namespace declarations.
532  //
533  // We consider declarations inside a class belongs to the class. So these
534  // declarations will be ignored.
535  auto AllDeclsInHeader = namedDecl(
536  unless(ForwardClassDecls), unless(namespaceDecl()),
537  unless(usingDirectiveDecl()), // using namespace decl.
538  notInMacro(),
539  InOldHeader,
540  hasParent(decl(anyOf(namespaceDecl(), translationUnitDecl()))),
541  hasDeclContext(decl(anyOf(namespaceDecl(), translationUnitDecl()))));
542  Finder->addMatcher(AllDeclsInHeader.bind("decls_in_header"), this);
543 
544  // Don't register other matchers when dumping all declarations in header.
545  if (Context->DumpDeclarations)
546  return;
547 
548  // Match forward declarations in old header.
549  Finder->addMatcher(namedDecl(ForwardClassDecls, InOldHeader).bind("fwd_decl"),
550  this);
551 
552  //============================================================================
553  // Matchers for old cc
554  //============================================================================
555  auto IsOldCCTopLevelDecl = allOf(
556  hasParent(decl(anyOf(namespaceDecl(), translationUnitDecl()))), InOldCC);
557  // Matching using decls/type alias decls which are in named/anonymous/global
558  // namespace, these decls are always copied to new.h/cc. Those in classes,
559  // functions are covered in other matchers.
560  Finder->addMatcher(namedDecl(anyOf(usingDecl(IsOldCCTopLevelDecl),
561  usingDirectiveDecl(IsOldCCTopLevelDecl),
562  typeAliasDecl(IsOldCCTopLevelDecl)),
563  notInMacro())
564  .bind("using_decl"),
565  this);
566 
567  // Match static functions/variable definitions which are defined in named
568  // namespaces.
569  Optional<ast_matchers::internal::Matcher<NamedDecl>> HasAnySymbolNames;
570  for (StringRef SymbolName : Context->Spec.Names) {
571  llvm::StringRef GlobalSymbolName = SymbolName.trim().ltrim(':');
572  const auto HasName = hasName(("::" + GlobalSymbolName).str());
573  HasAnySymbolNames =
574  HasAnySymbolNames ? anyOf(*HasAnySymbolNames, HasName) : HasName;
575  }
576 
577  if (!HasAnySymbolNames) {
578  llvm::errs() << "No symbols being moved.\n";
579  return;
580  }
581  auto InMovedClass =
582  hasOutermostEnclosingClass(cxxRecordDecl(*HasAnySymbolNames));
583 
584  // Matchers for helper declarations in old.cc.
585  auto InAnonymousNS = hasParent(namespaceDecl(isAnonymous()));
586  auto NotInMovedClass= allOf(unless(InMovedClass), InOldCC);
587  auto IsOldCCHelper =
588  allOf(NotInMovedClass, anyOf(isStaticStorageClass(), InAnonymousNS));
589  // Match helper classes separately with helper functions/variables since we
590  // want to reuse these matchers in finding helpers usage below.
591  //
592  // There could be forward declarations usage for helpers, especially for
593  // classes and functions. We need include these forward declarations.
594  //
595  // Forward declarations for variable helpers will be excluded as these
596  // declarations (with "extern") are not supposed in cpp file.
597  auto HelperFuncOrVar =
598  namedDecl(notInMacro(), anyOf(functionDecl(IsOldCCHelper),
599  varDecl(isDefinition(), IsOldCCHelper)));
600  auto HelperClasses =
601  cxxRecordDecl(notInMacro(), NotInMovedClass, InAnonymousNS);
602  // Save all helper declarations in old.cc.
603  Finder->addMatcher(
604  namedDecl(anyOf(HelperFuncOrVar, HelperClasses)).bind("helper_decls"),
605  this);
606 
607  // Construct an AST-based call graph of helper declarations in old.cc.
608  // In the following matcheres, "dc" is a caller while "helper_decls" and
609  // "used_class" is a callee, so a new edge starting from caller to callee will
610  // be add in the graph.
611  //
612  // Find helper function/variable usages.
613  Finder->addMatcher(
614  declRefExpr(to(HelperFuncOrVar), hasAncestor(decl().bind("dc")))
615  .bind("func_ref"),
616  &RGBuilder);
617  // Find helper class usages.
618  Finder->addMatcher(
619  typeLoc(loc(recordType(hasDeclaration(HelperClasses.bind("used_class")))),
620  hasAncestor(decl().bind("dc"))),
621  &RGBuilder);
622 
623  //============================================================================
624  // Matchers for old files, including old.h/old.cc
625  //============================================================================
626  // Create a MatchCallback for class declarations.
627  MatchCallbacks.push_back(llvm::make_unique<ClassDeclarationMatch>(this));
628  // Match moved class declarations.
629  auto MovedClass = cxxRecordDecl(InOldFiles, *HasAnySymbolNames,
630  isDefinition(), TopLevelDecl)
631  .bind("moved_class");
632  Finder->addMatcher(MovedClass, MatchCallbacks.back().get());
633  // Match moved class methods (static methods included) which are defined
634  // outside moved class declaration.
635  Finder->addMatcher(
636  cxxMethodDecl(InOldFiles, ofOutermostEnclosingClass(*HasAnySymbolNames),
637  isDefinition())
638  .bind("class_method"),
639  MatchCallbacks.back().get());
640  // Match static member variable definition of the moved class.
641  Finder->addMatcher(
642  varDecl(InMovedClass, InOldFiles, isDefinition(), isStaticDataMember())
643  .bind("class_static_var_decl"),
644  MatchCallbacks.back().get());
645 
646  MatchCallbacks.push_back(llvm::make_unique<FunctionDeclarationMatch>(this));
647  Finder->addMatcher(functionDecl(InOldFiles, *HasAnySymbolNames, TopLevelDecl)
648  .bind("function"),
649  MatchCallbacks.back().get());
650 
651  MatchCallbacks.push_back(llvm::make_unique<VarDeclarationMatch>(this));
652  Finder->addMatcher(
653  varDecl(InOldFiles, *HasAnySymbolNames, TopLevelDecl).bind("var"),
654  MatchCallbacks.back().get());
655 
656  // Match enum definition in old.h. Enum helpers (which are defined in old.cc)
657  // will not be moved for now no matter whether they are used or not.
658  MatchCallbacks.push_back(llvm::make_unique<EnumDeclarationMatch>(this));
659  Finder->addMatcher(
660  enumDecl(InOldHeader, *HasAnySymbolNames, isDefinition(), TopLevelDecl)
661  .bind("enum"),
662  MatchCallbacks.back().get());
663 
664  // Match type alias in old.h, this includes "typedef" and "using" type alias
665  // declarations. Type alias helpers (which are defined in old.cc) will not be
666  // moved for now no matter whether they are used or not.
667  MatchCallbacks.push_back(llvm::make_unique<TypeAliasMatch>(this));
668  Finder->addMatcher(namedDecl(anyOf(typedefDecl().bind("typedef"),
669  typeAliasDecl().bind("type_alias")),
670  InOldHeader, *HasAnySymbolNames, TopLevelDecl),
671  MatchCallbacks.back().get());
672 }
673 
674 void ClangMoveTool::run(const ast_matchers::MatchFinder::MatchResult &Result) {
675  if (const auto *D =
676  Result.Nodes.getNodeAs<clang::NamedDecl>("decls_in_header")) {
677  UnremovedDeclsInOldHeader.insert(D);
678  } else if (const auto *FWD =
679  Result.Nodes.getNodeAs<clang::CXXRecordDecl>("fwd_decl")) {
680  // Skip all forward declarations which appear after moved class declaration.
681  if (RemovedDecls.empty()) {
682  if (const auto *DCT = FWD->getDescribedClassTemplate())
683  MovedDecls.push_back(DCT);
684  else
685  MovedDecls.push_back(FWD);
686  }
687  } else if (const auto *ND =
688  Result.Nodes.getNodeAs<clang::NamedDecl>("helper_decls")) {
689  MovedDecls.push_back(ND);
690  HelperDeclarations.push_back(ND);
691  LLVM_DEBUG(llvm::dbgs() << "Add helper : " << ND->getNameAsString() << " ("
692  << ND << ")\n");
693  } else if (const auto *UD =
694  Result.Nodes.getNodeAs<clang::NamedDecl>("using_decl")) {
695  MovedDecls.push_back(UD);
696  }
697 }
698 
699 std::string ClangMoveTool::makeAbsolutePath(StringRef Path) {
700  return MakeAbsolutePath(Context->OriginalRunningDirectory, Path);
701 }
702 
703 void ClangMoveTool::addIncludes(llvm::StringRef IncludeHeader, bool IsAngled,
704  llvm::StringRef SearchPath,
705  llvm::StringRef FileName,
706  clang::CharSourceRange IncludeFilenameRange,
707  const SourceManager &SM) {
708  SmallVector<char, 128> HeaderWithSearchPath;
709  llvm::sys::path::append(HeaderWithSearchPath, SearchPath, IncludeHeader);
710  std::string AbsoluteIncludeHeader =
711  MakeAbsolutePath(SM, llvm::StringRef(HeaderWithSearchPath.data(),
712  HeaderWithSearchPath.size()));
713  std::string IncludeLine =
714  IsAngled ? ("#include <" + IncludeHeader + ">\n").str()
715  : ("#include \"" + IncludeHeader + "\"\n").str();
716 
717  std::string AbsoluteOldHeader = makeAbsolutePath(Context->Spec.OldHeader);
718  std::string AbsoluteCurrentFile = MakeAbsolutePath(SM, FileName);
719  if (AbsoluteOldHeader == AbsoluteCurrentFile) {
720  // Find old.h includes "old.h".
721  if (AbsoluteOldHeader == AbsoluteIncludeHeader) {
722  OldHeaderIncludeRangeInHeader = IncludeFilenameRange;
723  return;
724  }
725  HeaderIncludes.push_back(IncludeLine);
726  } else if (makeAbsolutePath(Context->Spec.OldCC) == AbsoluteCurrentFile) {
727  // Find old.cc includes "old.h".
728  if (AbsoluteOldHeader == AbsoluteIncludeHeader) {
729  OldHeaderIncludeRangeInCC = IncludeFilenameRange;
730  return;
731  }
732  CCIncludes.push_back(IncludeLine);
733  }
734 }
735 
736 void ClangMoveTool::removeDeclsInOldFiles() {
737  if (RemovedDecls.empty()) return;
738 
739  // If old_header is not specified (only move declarations from old.cc), remain
740  // all the helper function declarations in old.cc as UnremovedDeclsInOldHeader
741  // is empty in this case, there is no way to verify unused/used helpers.
742  if (!Context->Spec.OldHeader.empty()) {
743  std::vector<const NamedDecl *> UnremovedDecls;
744  for (const auto *D : UnremovedDeclsInOldHeader)
745  UnremovedDecls.push_back(D);
746 
747  auto UsedDecls = getUsedDecls(RGBuilder.getGraph(), UnremovedDecls);
748 
749  // We remove the helper declarations which are not used in the old.cc after
750  // moving the given declarations.
751  for (const auto *D : HelperDeclarations) {
752  LLVM_DEBUG(llvm::dbgs() << "Check helper is used: "
753  << D->getNameAsString() << " (" << D << ")\n");
755  D->getCanonicalDecl()))) {
756  LLVM_DEBUG(llvm::dbgs() << "Helper removed in old.cc: "
757  << D->getNameAsString() << " (" << D << ")\n");
758  RemovedDecls.push_back(D);
759  }
760  }
761  }
762 
763  for (const auto *RemovedDecl : RemovedDecls) {
764  const auto &SM = RemovedDecl->getASTContext().getSourceManager();
765  auto Range = getFullRange(RemovedDecl);
766  clang::tooling::Replacement RemoveReplacement(
767  SM,
768  clang::CharSourceRange::getCharRange(Range.getBegin(), Range.getEnd()),
769  "");
770  std::string FilePath = RemoveReplacement.getFilePath().str();
771  auto Err = Context->FileToReplacements[FilePath].add(RemoveReplacement);
772  if (Err)
773  llvm::errs() << llvm::toString(std::move(Err)) << "\n";
774  }
775  const auto &SM = RemovedDecls[0]->getASTContext().getSourceManager();
776 
777  // Post process of cleanup around all the replacements.
778  for (auto &FileAndReplacements : Context->FileToReplacements) {
779  StringRef FilePath = FileAndReplacements.first;
780  // Add #include of new header to old header.
781  if (Context->Spec.OldDependOnNew &&
782  MakeAbsolutePath(SM, FilePath) ==
783  makeAbsolutePath(Context->Spec.OldHeader)) {
784  // FIXME: Minimize the include path like include-fixer.
785  std::string IncludeNewH =
786  "#include \"" + Context->Spec.NewHeader + "\"\n";
787  // This replacment for inserting header will be cleaned up at the end.
788  auto Err = FileAndReplacements.second.add(
789  tooling::Replacement(FilePath, UINT_MAX, 0, IncludeNewH));
790  if (Err)
791  llvm::errs() << llvm::toString(std::move(Err)) << "\n";
792  }
793 
794  auto SI = FilePathToFileID.find(FilePath);
795  // Ignore replacements for new.h/cc.
796  if (SI == FilePathToFileID.end()) continue;
797  llvm::StringRef Code = SM.getBufferData(SI->second);
798  auto Style = format::getStyle("file", FilePath, Context->FallbackStyle);
799  if (!Style) {
800  llvm::errs() << llvm::toString(Style.takeError()) << "\n";
801  continue;
802  }
803  auto CleanReplacements = format::cleanupAroundReplacements(
804  Code, Context->FileToReplacements[FilePath], *Style);
805 
806  if (!CleanReplacements) {
807  llvm::errs() << llvm::toString(CleanReplacements.takeError()) << "\n";
808  continue;
809  }
810  Context->FileToReplacements[FilePath] = *CleanReplacements;
811  }
812 }
813 
814 void ClangMoveTool::moveDeclsToNewFiles() {
815  std::vector<const NamedDecl *> NewHeaderDecls;
816  std::vector<const NamedDecl *> NewCCDecls;
817  for (const auto *MovedDecl : MovedDecls) {
818  if (isInHeaderFile(MovedDecl, Context->OriginalRunningDirectory,
819  Context->Spec.OldHeader))
820  NewHeaderDecls.push_back(MovedDecl);
821  else
822  NewCCDecls.push_back(MovedDecl);
823  }
824 
825  auto UsedDecls = getUsedDecls(RGBuilder.getGraph(), RemovedDecls);
826  std::vector<const NamedDecl *> ActualNewCCDecls;
827 
828  // Filter out all unused helpers in NewCCDecls.
829  // We only move the used helpers (including transively used helpers) and the
830  // given symbols being moved.
831  for (const auto *D : NewCCDecls) {
832  if (llvm::is_contained(HelperDeclarations, D) &&
834  D->getCanonicalDecl())))
835  continue;
836 
837  LLVM_DEBUG(llvm::dbgs() << "Helper used in new.cc: " << D->getNameAsString()
838  << " " << D << "\n");
839  ActualNewCCDecls.push_back(D);
840  }
841 
842  if (!Context->Spec.NewHeader.empty()) {
843  std::string OldHeaderInclude =
844  Context->Spec.NewDependOnOld
845  ? "#include \"" + Context->Spec.OldHeader + "\"\n"
846  : "";
847  Context->FileToReplacements[Context->Spec.NewHeader] =
848  createInsertedReplacements(HeaderIncludes, NewHeaderDecls,
849  Context->Spec.NewHeader, /*IsHeader=*/true,
850  OldHeaderInclude);
851  }
852  if (!Context->Spec.NewCC.empty())
853  Context->FileToReplacements[Context->Spec.NewCC] =
854  createInsertedReplacements(CCIncludes, ActualNewCCDecls,
855  Context->Spec.NewCC);
856 }
857 
858 // Move all contents from OldFile to NewFile.
859 void ClangMoveTool::moveAll(SourceManager &SM, StringRef OldFile,
860  StringRef NewFile) {
861  const FileEntry *FE = SM.getFileManager().getFile(makeAbsolutePath(OldFile));
862  if (!FE) {
863  llvm::errs() << "Failed to get file: " << OldFile << "\n";
864  return;
865  }
866  FileID ID = SM.getOrCreateFileID(FE, SrcMgr::C_User);
867  auto Begin = SM.getLocForStartOfFile(ID);
868  auto End = SM.getLocForEndOfFile(ID);
869  clang::tooling::Replacement RemoveAll (
870  SM, clang::CharSourceRange::getCharRange(Begin, End), "");
871  std::string FilePath = RemoveAll.getFilePath().str();
872  Context->FileToReplacements[FilePath] =
873  clang::tooling::Replacements(RemoveAll);
874 
875  StringRef Code = SM.getBufferData(ID);
876  if (!NewFile.empty()) {
877  auto AllCode = clang::tooling::Replacements(
878  clang::tooling::Replacement(NewFile, 0, 0, Code));
879  auto ReplaceOldInclude = [&](clang::CharSourceRange OldHeaderIncludeRange) {
880  AllCode = AllCode.merge(clang::tooling::Replacements(
881  clang::tooling::Replacement(SM, OldHeaderIncludeRange,
882  '"' + Context->Spec.NewHeader + '"')));
883  };
884  // Fix the case where old.h/old.cc includes "old.h", we replace the
885  // `#include "old.h"` with `#include "new.h"`.
886  if (Context->Spec.NewCC == NewFile && OldHeaderIncludeRangeInCC.isValid())
887  ReplaceOldInclude(OldHeaderIncludeRangeInCC);
888  else if (Context->Spec.NewHeader == NewFile &&
889  OldHeaderIncludeRangeInHeader.isValid())
890  ReplaceOldInclude(OldHeaderIncludeRangeInHeader);
891  Context->FileToReplacements[NewFile] = std::move(AllCode);
892  }
893 }
894 
896  if (Context->DumpDeclarations) {
897  assert(Reporter);
898  for (const auto *Decl : UnremovedDeclsInOldHeader) {
899  auto Kind = Decl->getKind();
900  const std::string QualifiedName = Decl->getQualifiedNameAsString();
901  if (Kind == Decl::Kind::Var)
902  Reporter->reportDeclaration(QualifiedName, "Variable");
903  else if (Kind == Decl::Kind::Function ||
904  Kind == Decl::Kind::FunctionTemplate)
905  Reporter->reportDeclaration(QualifiedName, "Function");
906  else if (Kind == Decl::Kind::ClassTemplate ||
907  Kind == Decl::Kind::CXXRecord)
908  Reporter->reportDeclaration(QualifiedName, "Class");
909  else if (Kind == Decl::Kind::Enum)
910  Reporter->reportDeclaration(QualifiedName, "Enum");
911  else if (Kind == Decl::Kind::Typedef ||
912  Kind == Decl::Kind::TypeAlias ||
913  Kind == Decl::Kind::TypeAliasTemplate)
914  Reporter->reportDeclaration(QualifiedName, "TypeAlias");
915  }
916  return;
917  }
918 
919  if (RemovedDecls.empty())
920  return;
921  // Ignore symbols that are not supported when checking if there is unremoved
922  // symbol in old header. This makes sure that we always move old files to new
923  // files when all symbols produced from dump_decls are moved.
924  auto IsSupportedKind = [](const clang::NamedDecl *Decl) {
925  switch (Decl->getKind()) {
926  case Decl::Kind::Function:
927  case Decl::Kind::FunctionTemplate:
928  case Decl::Kind::ClassTemplate:
929  case Decl::Kind::CXXRecord:
930  case Decl::Kind::Enum:
931  case Decl::Kind::Typedef:
932  case Decl::Kind::TypeAlias:
933  case Decl::Kind::TypeAliasTemplate:
934  case Decl::Kind::Var:
935  return true;
936  default:
937  return false;
938  }
939  };
940  if (std::none_of(UnremovedDeclsInOldHeader.begin(),
941  UnremovedDeclsInOldHeader.end(), IsSupportedKind) &&
942  !Context->Spec.OldHeader.empty()) {
943  auto &SM = RemovedDecls[0]->getASTContext().getSourceManager();
944  moveAll(SM, Context->Spec.OldHeader, Context->Spec.NewHeader);
945  moveAll(SM, Context->Spec.OldCC, Context->Spec.NewCC);
946  return;
947  }
948  LLVM_DEBUG(RGBuilder.getGraph()->dump());
949  moveDeclsToNewFiles();
950  removeDeclsInOldFiles();
951 }
952 
953 } // namespace move
954 } // namespace clang
SourceLocation Loc
&#39;#&#39; location in the include directive
AST_MATCHER(BinaryOperator, isAssignmentOperator)
Definition: Matchers.h:20
void onEndOfTranslationUnit() override
Definition: ClangMove.cpp:895
std::string OriginalRunningDirectory
Definition: ClangMove.h:84
HeaderHandle File
MoveDefinitionSpec Spec
Definition: ClangMove.h:76
void reportDeclaration(llvm::StringRef DeclarationName, llvm::StringRef Type)
Definition: ClangMove.h:34
llvm::DenseSet< const CallGraphNode * > getReachableNodes(const Decl *D) const
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:24
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
std::vector< CodeCompletionResult > Results
def make_absolute(f, directory)
SmallVector< std::string, 4 > Names
Definition: ClangMove.h:57
std::vector< HeaderHandle > Path
void registerMatchers(ast_matchers::MatchFinder *Finder)
Definition: ClangMove.cpp:514
BindArgumentKind Kind
llvm::SmallPtrSet< const NamedDecl *, 8 > & getUnremovedDeclsInOldHeader()
Definition: ClangMove.h:143
static const Decl * getOutmostClassOrFunDecl(const Decl *D)
bool IsAngled
true if this was an include with angle brackets
PathRef FileName
const HelperDeclRefGraph * getGraph() const
std::map< std::string, tooling::Replacements > & FileToReplacements
Definition: ClangMove.h:78
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
AST_MATCHER_P(FunctionDecl, throws, internal::Matcher< Type >, InnerMatcher)
void run(const ast_matchers::MatchFinder::MatchResult &Result) override
Definition: ClangMove.cpp:674
CharSourceRange Range
SourceRange for the file name.
void addIncludes(llvm::StringRef IncludeHeader, bool IsAngled, llvm::StringRef SearchPath, llvm::StringRef FileName, clang::CharSourceRange IncludeFilenameRange, const SourceManager &SM)
Add #includes from old.h/cc files.
Definition: ClangMove.cpp:703
void addRemovedDecl(const NamedDecl *Decl)
Add declarations being removed from old.h/cc.
Definition: ClangMove.cpp:506
std::vector< const NamedDecl * > & getMovedDecls()
Definition: ClangMove.h:136