r366469 - [LibTooling] Add function to translate and validate source range for editing
Yitzhak Mandelbaum via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 18 10:26:58 PDT 2019
Author: ymandel
Date: Thu Jul 18 10:26:57 2019
New Revision: 366469
URL: http://llvm.org/viewvc/llvm-project?rev=366469&view=rev
Log:
[LibTooling] Add function to translate and validate source range for editing
Summary:
Adds the function `getRangeForEdit` to validate that a given source range is
editable and, if needed, translate it into a range in the source file (for
example, if it's sourced in macro expansions).
Reviewers: ilya-biryukov
Subscribers: cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64924
Modified:
cfe/trunk/include/clang/Tooling/Refactoring/SourceCode.h
cfe/trunk/lib/Tooling/Refactoring/SourceCode.cpp
cfe/trunk/unittests/Tooling/SourceCodeTest.cpp
Modified: cfe/trunk/include/clang/Tooling/Refactoring/SourceCode.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Refactoring/SourceCode.h?rev=366469&r1=366468&r2=366469&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/Refactoring/SourceCode.h (original)
+++ cfe/trunk/include/clang/Tooling/Refactoring/SourceCode.h Thu Jul 18 10:26:57 2019
@@ -72,6 +72,19 @@ StringRef getExtendedText(const T &Node,
ASTContext &Context) {
return getText(getExtendedRange(Node, Next, Context), Context);
}
+
+// Attempts to resolve the given range to one that can be edited by a rewrite;
+// generally, one that starts and ends within a particular file. It supports
+// a limited set of cases involving source locations in macro expansions.
+llvm::Optional<CharSourceRange>
+getRangeForEdit(const CharSourceRange &EditRange, const SourceManager &SM,
+ const LangOptions &LangOpts);
+
+inline llvm::Optional<CharSourceRange>
+getRangeForEdit(const CharSourceRange &EditRange, const ASTContext &Context) {
+ return getRangeForEdit(EditRange, Context.getSourceManager(),
+ Context.getLangOpts());
+}
} // namespace tooling
} // namespace clang
#endif // LLVM_CLANG_TOOLING_REFACTOR_SOURCE_CODE_H
Modified: cfe/trunk/lib/Tooling/Refactoring/SourceCode.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Refactoring/SourceCode.cpp?rev=366469&r1=366468&r2=366469&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/Refactoring/SourceCode.cpp (original)
+++ cfe/trunk/lib/Tooling/Refactoring/SourceCode.cpp Thu Jul 18 10:26:57 2019
@@ -29,3 +29,37 @@ CharSourceRange clang::tooling::maybeExt
return Range;
return CharSourceRange::getTokenRange(Range.getBegin(), Tok->getLocation());
}
+
+llvm::Optional<CharSourceRange>
+clang::tooling::getRangeForEdit(const CharSourceRange &EditRange,
+ const SourceManager &SM,
+ const LangOptions &LangOpts) {
+ // FIXME: makeFileCharRange() has the disadvantage of stripping off "identity"
+ // macros. For example, if we're looking to rewrite the int literal 3 to 6,
+ // and we have the following definition:
+ // #define DO_NOTHING(x) x
+ // then
+ // foo(DO_NOTHING(3))
+ // will be rewritten to
+ // foo(6)
+ // rather than the arguably better
+ // foo(DO_NOTHING(6))
+ // Decide whether the current behavior is desirable and modify if not.
+ CharSourceRange Range = Lexer::makeFileCharRange(EditRange, SM, LangOpts);
+ if (Range.isInvalid())
+ return None;
+
+ if (Range.getBegin().isMacroID() || Range.getEnd().isMacroID())
+ return None;
+ if (SM.isInSystemHeader(Range.getBegin()) ||
+ SM.isInSystemHeader(Range.getEnd()))
+ return None;
+
+ std::pair<FileID, unsigned> BeginInfo = SM.getDecomposedLoc(Range.getBegin());
+ std::pair<FileID, unsigned> EndInfo = SM.getDecomposedLoc(Range.getEnd());
+ if (BeginInfo.first != EndInfo.first ||
+ BeginInfo.second > EndInfo.second)
+ return None;
+
+ return Range;
+}
Modified: cfe/trunk/unittests/Tooling/SourceCodeTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/SourceCodeTest.cpp?rev=366469&r1=366468&r2=366469&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/SourceCodeTest.cpp (original)
+++ cfe/trunk/unittests/Tooling/SourceCodeTest.cpp Thu Jul 18 10:26:57 2019
@@ -6,17 +6,32 @@
//
//===----------------------------------------------------------------------===//
+#include "clang/Tooling/Refactoring/SourceCode.h"
#include "TestVisitor.h"
#include "clang/Basic/Diagnostic.h"
-#include "clang/Tooling/Refactoring/SourceCode.h"
+#include "llvm/Testing/Support/Annotations.h"
+#include "llvm/Testing/Support/SupportHelpers.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
using namespace clang;
-using tooling::getText;
+using llvm::ValueIs;
using tooling::getExtendedText;
+using tooling::getRangeForEdit;
+using tooling::getText;
namespace {
+struct IntLitVisitor : TestVisitor<IntLitVisitor> {
+ bool VisitIntegerLiteral(IntegerLiteral *Expr) {
+ OnIntLit(Expr, Context);
+ return true;
+ }
+
+ std::function<void(IntegerLiteral *, ASTContext *Context)> OnIntLit;
+};
+
struct CallsVisitor : TestVisitor<CallsVisitor> {
bool VisitCallExpr(CallExpr *Expr) {
OnCall(Expr, Context);
@@ -26,6 +41,19 @@ struct CallsVisitor : TestVisitor<CallsV
std::function<void(CallExpr *, ASTContext *Context)> OnCall;
};
+// Equality matcher for `clang::CharSourceRange`, which lacks `operator==`.
+MATCHER_P(EqualsRange, R, "") {
+ return arg.isTokenRange() == R.isTokenRange() &&
+ arg.getBegin() == R.getBegin() && arg.getEnd() == R.getEnd();
+}
+
+static ::testing::Matcher<CharSourceRange> AsRange(const SourceManager &SM,
+ llvm::Annotations::Range R) {
+ return EqualsRange(CharSourceRange::getCharRange(
+ SM.getLocForStartOfFile(SM.getMainFileID()).getLocWithOffset(R.Begin),
+ SM.getLocForStartOfFile(SM.getMainFileID()).getLocWithOffset(R.End)));
+}
+
TEST(SourceCodeTest, getText) {
CallsVisitor Visitor;
@@ -94,4 +122,82 @@ TEST(SourceCodeTest, getExtendedText) {
Visitor.runOver("int foo() { return foo() + 3; }");
}
+TEST(SourceCodeTest, EditRangeWithMacroExpansionsShouldSucceed) {
+ // The call expression, whose range we are extracting, includes two macro
+ // expansions.
+ llvm::Annotations Code(R"cpp(
+#define M(a) a * 13
+int foo(int x, int y);
+int a = $r[[foo(M(1), M(2))]];
+)cpp");
+
+ CallsVisitor Visitor;
+
+ Visitor.OnCall = [&Code](CallExpr *CE, ASTContext *Context) {
+ auto Range = CharSourceRange::getTokenRange(CE->getSourceRange());
+ EXPECT_THAT(getRangeForEdit(Range, *Context),
+ ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
+ };
+ Visitor.runOver(Code.code());
+}
+
+TEST(SourceCodeTest, EditWholeMacroExpansionShouldSucceed) {
+ llvm::Annotations Code(R"cpp(
+#define FOO 10
+int a = $r[[FOO]];
+)cpp");
+
+ IntLitVisitor Visitor;
+ Visitor.OnIntLit = [&Code](IntegerLiteral *Expr, ASTContext *Context) {
+ auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
+ EXPECT_THAT(getRangeForEdit(Range, *Context),
+ ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
+ };
+ Visitor.runOver(Code.code());
+}
+
+TEST(SourceCodeTest, EditPartialMacroExpansionShouldFail) {
+ std::string Code = R"cpp(
+#define BAR 10+
+int c = BAR 3.0;
+)cpp";
+
+ IntLitVisitor Visitor;
+ Visitor.OnIntLit = [](IntegerLiteral *Expr, ASTContext *Context) {
+ auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
+ EXPECT_FALSE(getRangeForEdit(Range, *Context).hasValue());
+ };
+ Visitor.runOver(Code);
+}
+
+TEST(SourceCodeTest, EditWholeMacroArgShouldSucceed) {
+ llvm::Annotations Code(R"cpp(
+#define FOO(a) a + 7.0;
+int a = FOO($r[[10]]);
+)cpp");
+
+ IntLitVisitor Visitor;
+ Visitor.OnIntLit = [&Code](IntegerLiteral *Expr, ASTContext *Context) {
+ auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
+ EXPECT_THAT(getRangeForEdit(Range, *Context),
+ ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
+ };
+ Visitor.runOver(Code.code());
+}
+
+TEST(SourceCodeTest, EditPartialMacroArgShouldSucceed) {
+ llvm::Annotations Code(R"cpp(
+#define FOO(a) a + 7.0;
+int a = FOO($r[[10]] + 10.0);
+)cpp");
+
+ IntLitVisitor Visitor;
+ Visitor.OnIntLit = [&Code](IntegerLiteral *Expr, ASTContext *Context) {
+ auto Range = CharSourceRange::getTokenRange(Expr->getSourceRange());
+ EXPECT_THAT(getRangeForEdit(Range, *Context),
+ ValueIs(AsRange(Context->getSourceManager(), Code.range("r"))));
+ };
+ Visitor.runOver(Code.code());
+}
+
} // end anonymous namespace
More information about the cfe-commits
mailing list