diff options
-rw-r--r-- | clang-tools-extra/clang-rename/RenamingAction.cpp | 3 | ||||
-rw-r--r-- | clang-tools-extra/clang-rename/USRFinder.cpp | 26 | ||||
-rw-r--r-- | clang-tools-extra/clang-rename/USRFindingAction.cpp | 43 | ||||
-rw-r--r-- | clang-tools-extra/clang-rename/USRLocFinder.cpp | 33 |
4 files changed, 43 insertions, 62 deletions
diff --git a/clang-tools-extra/clang-rename/RenamingAction.cpp b/clang-tools-extra/clang-rename/RenamingAction.cpp index 6fb8b145175..08944552ac4 100644 --- a/clang-tools-extra/clang-rename/RenamingAction.cpp +++ b/clang-tools-extra/clang-rename/RenamingAction.cpp @@ -44,9 +44,8 @@ public: FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {} void HandleTranslationUnit(ASTContext &Context) override { - for (unsigned I = 0; I < NewNames.size(); ++I) { + for (unsigned I = 0; I < NewNames.size(); ++I) HandleOneRename(Context, NewNames[I], PrevNames[I], USRList[I]); - } } void HandleOneRename(ASTContext &Context, const std::string &NewName, diff --git a/clang-tools-extra/clang-rename/USRFinder.cpp b/clang-tools-extra/clang-rename/USRFinder.cpp index 2766b5ae53e..494bc75b56b 100644 --- a/clang-tools-extra/clang-rename/USRFinder.cpp +++ b/clang-tools-extra/clang-rename/USRFinder.cpp @@ -78,9 +78,8 @@ public: const SourceLocation TypeEndLoc = Lexer::getLocForEndOfToken( TypeBeginLoc, 0, Context.getSourceManager(), Context.getLangOpts()); if (const auto *TemplateTypeParm = - dyn_cast<TemplateTypeParmType>(Loc.getType())) { + dyn_cast<TemplateTypeParmType>(Loc.getType())) return setResult(TemplateTypeParm->getDecl(), TypeBeginLoc, TypeEndLoc); - } if (const auto *TemplateSpecType = dyn_cast<TemplateSpecializationType>(Loc.getType())) { return setResult(TemplateSpecType->getTemplateName().getAsTemplateDecl(), @@ -92,18 +91,16 @@ public: bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) { for (const auto *Initializer : ConstructorDecl->inits()) { - if (!Initializer->isWritten()) { - // Ignore implicit initializers. + // Ignore implicit initializers. + if (!Initializer->isWritten()) continue; - } if (const clang::FieldDecl *FieldDecl = Initializer->getMember()) { const SourceLocation InitBeginLoc = Initializer->getSourceLocation(), InitEndLoc = Lexer::getLocForEndOfToken( InitBeginLoc, 0, Context.getSourceManager(), Context.getLangOpts()); - if (!setResult(FieldDecl, InitBeginLoc, InitEndLoc)) { + if (!setResult(FieldDecl, InitBeginLoc, InitEndLoc)) return false; - } } } return true; @@ -129,20 +126,17 @@ private: // \returns false on success. bool setResult(const NamedDecl *Decl, SourceLocation Start, SourceLocation End) { - if (!Decl) { + if (!Decl) return true; - } if (Name.empty()) { // Offset is used to find the declaration. if (!Start.isValid() || !Start.isFileID() || !End.isValid() || - !End.isFileID() || !isPointWithin(Start, End)) { + !End.isFileID() || !isPointWithin(Start, End)) return true; - } } else { // Fully qualified name is used to find the declaration. - if (Name != Decl->getQualifiedNameAsString()) { + if (Name != Decl->getQualifiedNameAsString()) return true; - } } Result = Decl; return false; @@ -182,15 +176,13 @@ const NamedDecl *getNamedDeclAt(const ASTContext &Context, const SourceLocation FileLoc = CurrDecl->getLocStart(); StringRef FileName = Context.getSourceManager().getFilename(FileLoc); // FIXME: Add test. - if (FileName == SearchFile) { + if (FileName == SearchFile) Visitor.TraverseDecl(CurrDecl); - } } NestedNameSpecifierLocFinder Finder(const_cast<ASTContext &>(Context)); - for (const auto &Location : Finder.getNestedNameSpecifierLocations()) { + for (const auto &Location : Finder.getNestedNameSpecifierLocations()) Visitor.handleNestedNameSpecifierLoc(Location); - } return Visitor.getNamedDecl(); } diff --git a/clang-tools-extra/clang-rename/USRFindingAction.cpp b/clang-tools-extra/clang-rename/USRFindingAction.cpp index c92e031ec2d..1383f8f834c 100644 --- a/clang-tools-extra/clang-rename/USRFindingAction.cpp +++ b/clang-tools-extra/clang-rename/USRFindingAction.cpp @@ -55,9 +55,8 @@ public: if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FoundDecl)) { addUSRsOfOverridenFunctions(MethodDecl); for (const auto &OverriddenMethod : OverriddenMethods) { - if (checkIfOverriddenFunctionAscends(OverriddenMethod)) { + if (checkIfOverriddenFunctionAscends(OverriddenMethod)) USRSet.insert(getUSRForDecl(OverriddenMethod)); - } } } else if (const auto *RecordDecl = dyn_cast<CXXRecordDecl>(FoundDecl)) { handleCXXRecordDecl(RecordDecl); @@ -71,9 +70,8 @@ public: } bool VisitCXXMethodDecl(const CXXMethodDecl *MethodDecl) { - if (MethodDecl->isVirtual()) { + if (MethodDecl->isVirtual()) OverriddenMethods.push_back(MethodDecl); - } return true; } @@ -87,46 +85,43 @@ private: void handleCXXRecordDecl(const CXXRecordDecl *RecordDecl) { RecordDecl = RecordDecl->getDefinition(); if (const auto *ClassTemplateSpecDecl = - dyn_cast<ClassTemplateSpecializationDecl>(RecordDecl)) { + dyn_cast<ClassTemplateSpecializationDecl>(RecordDecl)) handleClassTemplateDecl(ClassTemplateSpecDecl->getSpecializedTemplate()); - } addUSRsOfCtorDtors(RecordDecl); } void handleClassTemplateDecl(const ClassTemplateDecl *TemplateDecl) { - for (const auto *Specialization : TemplateDecl->specializations()) { + for (const auto *Specialization : TemplateDecl->specializations()) addUSRsOfCtorDtors(Specialization); - } + for (const auto *PartialSpec : PartialSpecs) { - if (PartialSpec->getSpecializedTemplate() == TemplateDecl) { + if (PartialSpec->getSpecializedTemplate() == TemplateDecl) addUSRsOfCtorDtors(PartialSpec); - } } addUSRsOfCtorDtors(TemplateDecl->getTemplatedDecl()); } void addUSRsOfCtorDtors(const CXXRecordDecl *RecordDecl) { RecordDecl = RecordDecl->getDefinition(); - for (const auto *CtorDecl : RecordDecl->ctors()) { + + for (const auto *CtorDecl : RecordDecl->ctors()) USRSet.insert(getUSRForDecl(CtorDecl)); - } + USRSet.insert(getUSRForDecl(RecordDecl->getDestructor())); USRSet.insert(getUSRForDecl(RecordDecl)); } void addUSRsOfOverridenFunctions(const CXXMethodDecl *MethodDecl) { USRSet.insert(getUSRForDecl(MethodDecl)); - for (const auto &OverriddenMethod : MethodDecl->overridden_methods()) { - // Recursively visit each OverridenMethod. + // Recursively visit each OverridenMethod. + for (const auto &OverriddenMethod : MethodDecl->overridden_methods()) addUSRsOfOverridenFunctions(OverriddenMethod); - } } bool checkIfOverriddenFunctionAscends(const CXXMethodDecl *MethodDecl) { for (const auto &OverriddenMethod : MethodDecl->overridden_methods()) { - if (USRSet.find(getUSRForDecl(OverriddenMethod)) != USRSet.end()) { + if (USRSet.find(getUSRForDecl(OverriddenMethod)) != USRSet.end()) return true; - } return checkIfOverriddenFunctionAscends(OverriddenMethod); } return false; @@ -151,11 +146,10 @@ struct NamedDeclFindingConsumer : public ASTConsumer { if (!Point.isValid()) return; const NamedDecl *FoundDecl = nullptr; - if (OldName.empty()) { + if (OldName.empty()) FoundDecl = getNamedDeclAt(Context, Point); - } else { + else FoundDecl = getNamedDeclFor(Context, OldName); - } if (FoundDecl == nullptr) { if (OldName.empty()) { FullSourceLoc FullLoc(Point, SourceMgr); @@ -164,18 +158,19 @@ struct NamedDeclFindingConsumer : public ASTConsumer { << FullLoc.getSpellingLineNumber() << ":" << FullLoc.getSpellingColumnNumber() << " (offset " << SymbolOffset << ").\n"; - } else + } else { errs() << "clang-rename: could not find symbol " << OldName << ".\n"; + } return; } // If FoundDecl is a constructor or destructor, we want to instead take the // Decl of the corresponding class. - if (const auto *CtorDecl = dyn_cast<CXXConstructorDecl>(FoundDecl)) { + if (const auto *CtorDecl = dyn_cast<CXXConstructorDecl>(FoundDecl)) FoundDecl = CtorDecl->getParent(); - } else if (const auto *DtorDecl = dyn_cast<CXXDestructorDecl>(FoundDecl)) { + else if (const auto *DtorDecl = dyn_cast<CXXDestructorDecl>(FoundDecl)) FoundDecl = DtorDecl->getParent(); - } + *SpellingName = FoundDecl->getNameAsString(); AdditionalUSRFinder Finder(FoundDecl, Context, USRs); diff --git a/clang-tools-extra/clang-rename/USRLocFinder.cpp b/clang-tools-extra/clang-rename/USRLocFinder.cpp index c7ce5338d07..60bc1c27c07 100644 --- a/clang-tools-extra/clang-rename/USRLocFinder.cpp +++ b/clang-tools-extra/clang-rename/USRLocFinder.cpp @@ -44,23 +44,20 @@ public: bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) { for (const auto *Initializer : ConstructorDecl->inits()) { - if (!Initializer->isWritten()) { - // Ignore implicit initializers. + // Ignore implicit initializers. + if (!Initializer->isWritten()) continue; - } if (const clang::FieldDecl *FieldDecl = Initializer->getMember()) { - if (USRSet.find(getUSRForDecl(FieldDecl)) != USRSet.end()) { + if (USRSet.find(getUSRForDecl(FieldDecl)) != USRSet.end()) LocationsFound.push_back(Initializer->getSourceLocation()); - } } } return true; } bool VisitNamedDecl(const NamedDecl *Decl) { - if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) { + if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) checkAndAddLocation(Decl->getLocation()); - } return true; } @@ -92,15 +89,13 @@ public: bool VisitTypeLoc(const TypeLoc Loc) { if (USRSet.find(getUSRForDecl(Loc.getType()->getAsCXXRecordDecl())) != - USRSet.end()) { + USRSet.end()) checkAndAddLocation(Loc.getBeginLoc()); - } if (const auto *TemplateTypeParm = dyn_cast<TemplateTypeParmType>(Loc.getType())) { if (USRSet.find(getUSRForDecl(TemplateTypeParm->getDecl())) != - USRSet.end()) { + USRSet.end()) checkAndAddLocation(Loc.getBeginLoc()); - } } return true; } @@ -118,9 +113,8 @@ public: while (NameLoc) { const NamespaceDecl *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace(); - if (Decl && USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) { + if (Decl && USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) checkAndAddLocation(NameLoc.getLocalBeginLoc()); - } NameLoc = NameLoc.getPrefix(); } } @@ -134,11 +128,11 @@ private: Lexer::getSourceText(CharSourceRange::getTokenRange(BeginLoc, EndLoc), Context.getSourceManager(), Context.getLangOpts()); size_t Offset = TokenName.find(PrevName); - if (Offset != StringRef::npos) { - // The token of the source location we find actually has the old - // name. + + // The token of the source location we find actually has the old + // name. + if (Offset != StringRef::npos) LocationsFound.push_back(BeginLoc.getLocWithOffset(Offset)); - } } const std::set<std::string> USRSet; @@ -154,9 +148,10 @@ getLocationsOfUSRs(const std::vector<std::string> &USRs, StringRef PrevName, USRLocFindingASTVisitor Visitor(USRs, PrevName, Decl->getASTContext()); Visitor.TraverseDecl(Decl); NestedNameSpecifierLocFinder Finder(Decl->getASTContext()); - for (const auto &Location : Finder.getNestedNameSpecifierLocations()) { + + for (const auto &Location : Finder.getNestedNameSpecifierLocations()) Visitor.handleNestedNameSpecifierLoc(Location); - } + return Visitor.getLocationsFound(); } |