[clang-tools-extra] r284236 - [clang-move] Don't overuse Replacements::add.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 14 06:43:49 PDT 2016
Author: hokein
Date: Fri Oct 14 08:43:49 2016
New Revision: 284236
URL: http://llvm.org/viewvc/llvm-project?rev=284236&view=rev
Log:
[clang-move] Don't overuse Replacements::add.
Reviewers: ioeric
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D25613
Modified:
clang-tools-extra/trunk/clang-move/ClangMove.cpp
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=284236&r1=284235&r2=284236&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-move/ClangMove.cpp (original)
+++ clang-tools-extra/trunk/clang-move/ClangMove.cpp Fri Oct 14 08:43:49 2016
@@ -193,27 +193,6 @@ std::string getDeclarationSourceText(con
return SourceText.str();
}
-clang::tooling::Replacement
-getReplacementInChangedCode(const clang::tooling::Replacements &Replacements,
- const clang::tooling::Replacement &Replacement) {
- unsigned Start = Replacements.getShiftedCodePosition(Replacement.getOffset());
- unsigned End = Replacements.getShiftedCodePosition(Replacement.getOffset() +
- Replacement.getLength());
- return clang::tooling::Replacement(Replacement.getFilePath(), Start,
- End - Start,
- Replacement.getReplacementText());
-}
-
-void addOrMergeReplacement(const clang::tooling::Replacement &Replacement,
- clang::tooling::Replacements *Replacements) {
- auto Err = Replacements->add(Replacement);
- if (Err) {
- llvm::consumeError(std::move(Err));
- auto Replace = getReplacementInChangedCode(*Replacements, Replacement);
- *Replacements = Replacements->merge(clang::tooling::Replacements(Replace));
- }
-}
-
bool isInHeaderFile(const clang::SourceManager &SM, const clang::Decl *D,
llvm::StringRef OriginalRunningDirectory,
llvm::StringRef OldHeader) {
@@ -251,7 +230,7 @@ createInsertedReplacements(const std::ve
const std::vector<ClangMoveTool::MovedDecl> &Decls,
llvm::StringRef FileName,
bool IsHeader = false) {
- clang::tooling::Replacements InsertedReplacements;
+ std::string NewCode;
std::string GuardName(FileName);
if (IsHeader) {
std::replace(GuardName.begin(), GuardName.end(), '/', '_');
@@ -259,24 +238,16 @@ createInsertedReplacements(const std::ve
std::replace(GuardName.begin(), GuardName.end(), '-', '_');
GuardName = StringRef(GuardName).upper();
- std::string HeaderGuard = "#ifndef " + GuardName + "\n";
- HeaderGuard += "#define " + GuardName + "\n";
- clang::tooling::Replacement HeaderGuardInclude(FileName, 0, 0,
- HeaderGuard);
- addOrMergeReplacement(HeaderGuardInclude, &InsertedReplacements);
+ NewCode += "#ifndef " + GuardName + "\n";
+ NewCode += "#define " + GuardName + "\n";
}
// Add #Includes.
- std::string AllIncludesString;
- // FIXME: Add header guard.
for (const auto &Include : Includes)
- AllIncludesString += Include;
+ NewCode += Include;
- if (!AllIncludesString.empty()) {
- clang::tooling::Replacement InsertInclude(FileName, 0, 0,
- AllIncludesString + "\n");
- addOrMergeReplacement(InsertInclude, &InsertedReplacements);
- }
+ if (!Includes.empty())
+ NewCode += "\n";
// Add moved class definition and its related declarations. All declarations
// in same namespace are grouped together.
@@ -299,36 +270,23 @@ createInsertedReplacements(const std::ve
for (auto It = CurrentNamespaces.rbegin(); RemainingSize > 0;
--RemainingSize, ++It) {
assert(It < CurrentNamespaces.rend());
- auto code = "} // namespace " + *It + "\n";
- clang::tooling::Replacement InsertedReplacement(FileName, 0, 0, code);
- addOrMergeReplacement(InsertedReplacement, &InsertedReplacements);
+ NewCode += "} // namespace " + *It + "\n";
}
while (DeclIt != DeclNamespaces.end()) {
- clang::tooling::Replacement InsertedReplacement(
- FileName, 0, 0, "namespace " + *DeclIt + " {\n");
- addOrMergeReplacement(InsertedReplacement, &InsertedReplacements);
+ NewCode += "namespace " + *DeclIt + " {\n";
++DeclIt;
}
-
- clang::tooling::Replacement InsertedReplacement(
- FileName, 0, 0, getDeclarationSourceText(MovedDecl.Decl, MovedDecl.SM));
- addOrMergeReplacement(InsertedReplacement, &InsertedReplacements);
-
+ NewCode += getDeclarationSourceText(MovedDecl.Decl, MovedDecl.SM);
CurrentNamespaces = std::move(NextNamespaces);
}
std::reverse(CurrentNamespaces.begin(), CurrentNamespaces.end());
- for (const auto &NS : CurrentNamespaces) {
- clang::tooling::Replacement InsertedReplacement(
- FileName, 0, 0, "} // namespace " + NS + "\n");
- addOrMergeReplacement(InsertedReplacement, &InsertedReplacements);
- }
+ for (const auto &NS : CurrentNamespaces)
+ NewCode += "} // namespace " + NS + "\n";
- if (IsHeader) {
- clang::tooling::Replacement HeaderGuardEnd(FileName, 0, 0,
- "#endif // " + GuardName + "\n");
- addOrMergeReplacement(HeaderGuardEnd, &InsertedReplacements);
- }
- return InsertedReplacements;
+ if (IsHeader)
+ NewCode += "#endif // " + GuardName + "\n";
+ return clang::tooling::Replacements(
+ clang::tooling::Replacement(FileName, 0, 0, NewCode));
}
} // namespace
@@ -490,7 +448,11 @@ void ClangMoveTool::removeClassDefinitio
Range.getBegin(), Range.getEnd()),
"");
std::string FilePath = RemoveReplacement.getFilePath().str();
- addOrMergeReplacement(RemoveReplacement, &FileToReplacements[FilePath]);
+ auto Err = FileToReplacements[FilePath].add(RemoveReplacement);
+ if (Err) {
+ llvm::errs() << llvm::toString(std::move(Err)) << "\n";
+ continue;
+ }
llvm::StringRef Code =
SM.getBufferData(SM.getFileID(MovedDecl.Decl->getLocation()));
More information about the cfe-commits
mailing list