#ifndef LLVM_CLANG_TOOLING_CORE_REPLACEMENT_H
#define LLVM_CLANG_TOOLING_CORE_REPLACEMENT_H
+#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceLocation.h"
#include "llvm/ADT/StringRef.h"
#include <set>
/// \param FilePath A source file accessible via a SourceManager.
/// \param Offset The byte offset of the start of the range in the file.
/// \param Length The length of the range in bytes.
- Replacement(StringRef FilePath, unsigned Offset,
- unsigned Length, StringRef ReplacementText);
+ Replacement(StringRef FilePath, unsigned Offset, unsigned Length,
+ StringRef ReplacementText);
/// \brief Creates a Replacement of the range [Start, Start+Length) with
/// ReplacementText.
- Replacement(const SourceManager &Sources, SourceLocation Start, unsigned Length,
- StringRef ReplacementText);
+ Replacement(const SourceManager &Sources, SourceLocation Start,
+ unsigned Length, StringRef ReplacementText);
/// \brief Creates a Replacement of the given range with ReplacementText.
Replacement(const SourceManager &Sources, const CharSourceRange &Range,
- StringRef ReplacementText);
+ StringRef ReplacementText,
+ const LangOptions &LangOpts = LangOptions());
/// \brief Creates a Replacement of the node with ReplacementText.
template <typename Node>
Replacement(const SourceManager &Sources, const Node &NodeToReplace,
- StringRef ReplacementText);
+ StringRef ReplacementText,
+ const LangOptions &LangOpts = LangOptions());
/// \brief Returns whether this replacement can be applied to a file.
///
std::string toString() const;
private:
- void setFromSourceLocation(const SourceManager &Sources, SourceLocation Start,
- unsigned Length, StringRef ReplacementText);
- void setFromSourceRange(const SourceManager &Sources,
- const CharSourceRange &Range,
- StringRef ReplacementText);
+ void setFromSourceLocation(const SourceManager &Sources,
+ SourceLocation Start, unsigned Length,
+ StringRef ReplacementText);
+ void setFromSourceRange(const SourceManager &Sources,
+ const CharSourceRange &Range,
+ StringRef ReplacementText,
+ const LangOptions &LangOpts);
std::string FilePath;
Range ReplacementRange;
template <typename Node>
Replacement::Replacement(const SourceManager &Sources,
- const Node &NodeToReplace, StringRef ReplacementText) {
+ const Node &NodeToReplace, StringRef ReplacementText,
+ const LangOptions &LangOpts) {
const CharSourceRange Range =
CharSourceRange::getTokenRange(NodeToReplace->getSourceRange());
- setFromSourceRange(Sources, Range, ReplacementText);
+ setFromSourceRange(Sources, Range, ReplacementText, LangOpts);
}
} // end namespace tooling
Replacement::Replacement(const SourceManager &Sources,
const CharSourceRange &Range,
- StringRef ReplacementText) {
- setFromSourceRange(Sources, Range, ReplacementText);
+ StringRef ReplacementText,
+ const LangOptions &LangOpts) {
+ setFromSourceRange(Sources, Range, ReplacementText, LangOpts);
}
bool Replacement::isApplicable() const {
// to handle ranges for refactoring in general first - there is no obvious
// good way how to integrate this into the Lexer yet.
static int getRangeSize(const SourceManager &Sources,
- const CharSourceRange &Range) {
+ const CharSourceRange &Range,
+ const LangOptions &LangOpts) {
SourceLocation SpellingBegin = Sources.getSpellingLoc(Range.getBegin());
SourceLocation SpellingEnd = Sources.getSpellingLoc(Range.getEnd());
std::pair<FileID, unsigned> Start = Sources.getDecomposedLoc(SpellingBegin);
std::pair<FileID, unsigned> End = Sources.getDecomposedLoc(SpellingEnd);
if (Start.first != End.first) return -1;
if (Range.isTokenRange())
- End.second += Lexer::MeasureTokenLength(SpellingEnd, Sources,
- LangOptions());
+ End.second += Lexer::MeasureTokenLength(SpellingEnd, Sources, LangOpts);
return End.second - Start.second;
}
void Replacement::setFromSourceRange(const SourceManager &Sources,
const CharSourceRange &Range,
- StringRef ReplacementText) {
+ StringRef ReplacementText,
+ const LangOptions &LangOpts) {
setFromSourceLocation(Sources, Sources.getSpellingLoc(Range.getBegin()),
- getRangeSize(Sources, Range), ReplacementText);
+ getRangeSize(Sources, Range, LangOpts),
+ ReplacementText);
}
unsigned shiftedCodePosition(const Replacements &Replaces, unsigned Position) {
protected:
clang::SourceManager *SM;
+ clang::ASTContext *Context;
private:
class FindConsumer : public clang::ASTConsumer {
CreateASTConsumer(clang::CompilerInstance &compiler,
llvm::StringRef dummy) override {
Visitor->SM = &compiler.getSourceManager();
+ Visitor->Context = &compiler.getASTContext();
/// TestConsumer will be deleted by the framework calling us.
return llvm::make_unique<FindConsumer>(Visitor);
}
expectReplacementAt(CallToF.Replace, "input.cc", 43, 8);
}
+class NestedNameSpecifierAVisitor
+ : public TestVisitor<NestedNameSpecifierAVisitor> {
+public:
+ bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSLoc) {
+ if (NNSLoc.getNestedNameSpecifier()) {
+ if (const NamespaceDecl* NS = NNSLoc.getNestedNameSpecifier()->getAsNamespace()) {
+ if (NS->getName() == "a") {
+ Replace = Replacement(*SM, &NNSLoc, "", Context->getLangOpts());
+ }
+ }
+ }
+ return TestVisitor<NestedNameSpecifierAVisitor>::TraverseNestedNameSpecifierLoc(
+ NNSLoc);
+ }
+ Replacement Replace;
+};
+
+TEST(Replacement, ColonColon) {
+ NestedNameSpecifierAVisitor VisitNNSA;
+ EXPECT_TRUE(VisitNNSA.runOver("namespace a { void f() { ::a::f(); } }"));
+ expectReplacementAt(VisitNNSA.Replace, "input.cc", 25, 5);
+}
+
TEST(Range, overlaps) {
EXPECT_TRUE(Range(10, 10).overlapsWith(Range(0, 11)));
EXPECT_TRUE(Range(0, 11).overlapsWith(Range(10, 10)));