-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clangd] Move Rename into its own file, and add unit test. NFC
Reviewers: kadircet Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, jfb, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D61596 llvm-svn: 360116
- llvmorg-20-init
- llvmorg-19.1.6
- llvmorg-19.1.5
- llvmorg-19.1.4
- llvmorg-19.1.3
- llvmorg-19.1.2
- llvmorg-19.1.1
- llvmorg-19.1.0
- llvmorg-19.1.0-rc4
- llvmorg-19.1.0-rc3
- llvmorg-19.1.0-rc2
- llvmorg-19.1.0-rc1
- llvmorg-19-init
- llvmorg-18.1.8
- llvmorg-18.1.7
- llvmorg-18.1.6
- llvmorg-18.1.5
- llvmorg-18.1.4
- llvmorg-18.1.3
- llvmorg-18.1.2
- llvmorg-18.1.1
- llvmorg-18.1.0
- llvmorg-18.1.0-rc4
- llvmorg-18.1.0-rc3
- llvmorg-18.1.0-rc2
- llvmorg-18.1.0-rc1
- llvmorg-18-init
- llvmorg-17.0.6
- llvmorg-17.0.5
- llvmorg-17.0.4
- llvmorg-17.0.3
- llvmorg-17.0.2
- llvmorg-17.0.1
- llvmorg-17.0.0
- llvmorg-17.0.0-rc4
- llvmorg-17.0.0-rc3
- llvmorg-17.0.0-rc2
- llvmorg-17.0.0-rc1
- llvmorg-17-init
- llvmorg-16.0.6
- llvmorg-16.0.5
- llvmorg-16.0.4
- llvmorg-16.0.3
- llvmorg-16.0.2
- llvmorg-16.0.1
- llvmorg-16.0.0
- llvmorg-16.0.0-rc4
- llvmorg-16.0.0-rc3
- llvmorg-16.0.0-rc2
- llvmorg-16.0.0-rc1
- llvmorg-16-init
- llvmorg-15.0.7
- llvmorg-15.0.6
- llvmorg-15.0.5
- llvmorg-15.0.4
- llvmorg-15.0.3
- llvmorg-15.0.2
- llvmorg-15.0.1
- llvmorg-15.0.0
- llvmorg-15.0.0-rc3
- llvmorg-15.0.0-rc2
- llvmorg-15.0.0-rc1
- llvmorg-15-init
- llvmorg-14.0.6
- llvmorg-14.0.5
- llvmorg-14.0.4
- llvmorg-14.0.3
- llvmorg-14.0.2
- llvmorg-14.0.1
- llvmorg-14.0.0
- llvmorg-14.0.0-rc4
- llvmorg-14.0.0-rc3
- llvmorg-14.0.0-rc2
- llvmorg-14.0.0-rc1
- llvmorg-14-init
- llvmorg-13.0.1
- llvmorg-13.0.1-rc3
- llvmorg-13.0.1-rc2
- llvmorg-13.0.1-rc1
- llvmorg-13.0.0
- llvmorg-13.0.0-rc4
- llvmorg-13.0.0-rc3
- llvmorg-13.0.0-rc2
- llvmorg-13.0.0-rc1
- llvmorg-13-init
- llvmorg-12.0.1
- llvmorg-12.0.1-rc4
- llvmorg-12.0.1-rc3
- llvmorg-12.0.1-rc2
- llvmorg-12.0.1-rc1
- llvmorg-12.0.0
- llvmorg-12.0.0-rc5
- llvmorg-12.0.0-rc4
- llvmorg-12.0.0-rc3
- llvmorg-12.0.0-rc2
- llvmorg-12.0.0-rc1
- llvmorg-12-init
- llvmorg-11.1.0
- llvmorg-11.1.0-rc3
- llvmorg-11.1.0-rc2
- llvmorg-11.1.0-rc1
- llvmorg-11.0.1
- llvmorg-11.0.1-rc2
- llvmorg-11.0.1-rc1
- llvmorg-11.0.0
- llvmorg-11.0.0-rc6
- llvmorg-11.0.0-rc5
- llvmorg-11.0.0-rc4
- llvmorg-11.0.0-rc3
- llvmorg-11.0.0-rc2
- llvmorg-11.0.0-rc1
- llvmorg-11-init
- llvmorg-10.0.1
- llvmorg-10.0.1-rc4
- llvmorg-10.0.1-rc3
- llvmorg-10.0.1-rc2
- llvmorg-10.0.1-rc1
- llvmorg-10.0.0
- llvmorg-10.0.0-rc6
- llvmorg-10.0.0-rc5
- llvmorg-10.0.0-rc4
- llvmorg-10.0.0-rc3
- llvmorg-10.0.0-rc2
- llvmorg-10.0.0-rc1
- llvmorg-10-init
- llvmorg-9.0.1
- llvmorg-9.0.1-rc3
- llvmorg-9.0.1-rc2
- llvmorg-9.0.1-rc1
- llvmorg-9.0.0
- llvmorg-9.0.0-rc6
- llvmorg-9.0.0-rc5
- llvmorg-9.0.0-rc4
- llvmorg-9.0.0-rc3
- llvmorg-9.0.0-rc2
- llvmorg-9.0.0-rc1
1 parent
b9de3eb
commit c094912
Showing
8 changed files
with
164 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include "refactor/Rename.h" | ||
#include "clang/Tooling/Refactoring/RefactoringResultConsumer.h" | ||
#include "clang/Tooling/Refactoring/Rename/RenamingAction.h" | ||
|
||
namespace clang { | ||
namespace clangd { | ||
namespace { | ||
|
||
class RefactoringResultCollector final | ||
: public tooling::RefactoringResultConsumer { | ||
public: | ||
void handleError(llvm::Error Err) override { | ||
assert(!Result.hasValue()); | ||
Result = std::move(Err); | ||
} | ||
|
||
// Using the handle(SymbolOccurrences) from parent class. | ||
using tooling::RefactoringResultConsumer::handle; | ||
|
||
void handle(tooling::AtomicChanges SourceReplacements) override { | ||
assert(!Result.hasValue()); | ||
Result = std::move(SourceReplacements); | ||
} | ||
|
||
llvm::Optional<llvm::Expected<tooling::AtomicChanges>> Result; | ||
}; | ||
|
||
// Expand a DiagnosticError to make it print-friendly (print the detailed | ||
// message, rather than "clang diagnostic"). | ||
llvm::Error expandDiagnostics(llvm::Error Err, DiagnosticsEngine &DE) { | ||
if (auto Diag = DiagnosticError::take(Err)) { | ||
llvm::cantFail(std::move(Err)); | ||
SmallVector<char, 128> DiagMessage; | ||
Diag->second.EmitToString(DE, DiagMessage); | ||
return llvm::make_error<llvm::StringError>(DiagMessage, | ||
llvm::inconvertibleErrorCode()); | ||
} | ||
return Err; | ||
} | ||
|
||
} // namespace | ||
|
||
llvm::Expected<tooling::Replacements> | ||
renameWithinFile(ParsedAST &AST, llvm::StringRef File, Position Pos, | ||
llvm::StringRef NewName) { | ||
RefactoringResultCollector ResultCollector; | ||
ASTContext &ASTCtx = AST.getASTContext(); | ||
const SourceManager &SourceMgr = ASTCtx.getSourceManager(); | ||
SourceLocation SourceLocationBeg = | ||
clangd::getBeginningOfIdentifier(AST, Pos, SourceMgr.getMainFileID()); | ||
tooling::RefactoringRuleContext Context(ASTCtx.getSourceManager()); | ||
Context.setASTContext(ASTCtx); | ||
auto Rename = clang::tooling::RenameOccurrences::initiate( | ||
Context, SourceRange(SourceLocationBeg), NewName); | ||
if (!Rename) | ||
return expandDiagnostics(Rename.takeError(), ASTCtx.getDiagnostics()); | ||
|
||
Rename->invoke(ResultCollector, Context); | ||
|
||
assert(ResultCollector.Result.hasValue()); | ||
if (!ResultCollector.Result.getValue()) | ||
return expandDiagnostics(ResultCollector.Result->takeError(), | ||
ASTCtx.getDiagnostics()); | ||
|
||
tooling::Replacements FilteredChanges; | ||
// Right now we only support renaming the main file, so we | ||
// drop replacements not for the main file. In the future, we might | ||
// also support rename with wider scope. | ||
for (const tooling::AtomicChange &Change : ResultCollector.Result->get()) { | ||
for (const auto &Rep : Change.getReplacements()) { | ||
if (Rep.getFilePath() == File) | ||
cantFail(FilteredChanges.add(Rep)); | ||
} | ||
} | ||
return FilteredChanges; | ||
} | ||
|
||
} // namespace clangd | ||
} // namespace clang |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//===--- Rename.h - Symbol-rename refactorings -------------------*- C++-*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "ClangdUnit.h" | ||
#include "clang/Tooling/Core/Replacement.h" | ||
#include "llvm/Support/Error.h" | ||
|
||
namespace clang { | ||
namespace clangd { | ||
|
||
/// Renames all occurrences of the symbol at \p Pos to \p NewName. | ||
/// Occurrences outside the current file are not modified. | ||
llvm::Expected<tooling::Replacements> renameWithinFile(ParsedAST &AST, | ||
llvm::StringRef File, | ||
Position Pos, | ||
llvm::StringRef NewName); | ||
|
||
} // namespace clangd | ||
} // namespace clang |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//===-- RenameTests.cpp -----------------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "Annotations.h" | ||
#include "TestFS.h" | ||
#include "TestTU.h" | ||
#include "refactor/Rename.h" | ||
#include "clang/Tooling/Core/Replacement.h" | ||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
|
||
namespace clang { | ||
namespace clangd { | ||
namespace { | ||
|
||
TEST(RenameTest, SingleFile) { | ||
Annotations Code(R"cpp( | ||
void foo() { | ||
fo^o(); | ||
} | ||
)cpp"); | ||
auto TU = TestTU::withCode(Code.code()); | ||
TU.HeaderCode = "void foo();"; // outside main file, will not be touched. | ||
|
||
auto AST = TU.build(); | ||
auto RenameResult = | ||
renameWithinFile(AST, testPath(TU.Filename), Code.point(), "abcde"); | ||
ASSERT_TRUE(bool(RenameResult)) << RenameResult.takeError(); | ||
auto ApplyResult = tooling::applyAllReplacements(Code.code(), *RenameResult); | ||
ASSERT_TRUE(bool(ApplyResult)) << ApplyResult.takeError(); | ||
|
||
const char *Want = R"cpp( | ||
void abcde() { | ||
abcde(); | ||
} | ||
)cpp"; | ||
EXPECT_EQ(Want, *ApplyResult); | ||
} | ||
|
||
} // namespace | ||
} // namespace clangd | ||
} // namespace clang |