summaryrefslogtreecommitdiffstats
path: root/clang/unittests/Tooling/SourceCodeTest.cpp
diff options
context:
space:
mode:
authorYitzhak Mandelbaum <yitzhakm@google.com>2019-07-18 17:26:57 +0000
committerYitzhak Mandelbaum <yitzhakm@google.com>2019-07-18 17:26:57 +0000
commit2e97a1e19ef5492a409d206bb544a746cd26360d (patch)
treea42b9ab74f13ff012147400069076ed5668c9d41 /clang/unittests/Tooling/SourceCodeTest.cpp
parentcfa14ac2a7768f90de3532833c88ccffd2ca0078 (diff)
downloadbcm5719-llvm-2e97a1e19ef5492a409d206bb544a746cd26360d.tar.gz
bcm5719-llvm-2e97a1e19ef5492a409d206bb544a746cd26360d.zip
[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 llvm-svn: 366469
Diffstat (limited to 'clang/unittests/Tooling/SourceCodeTest.cpp')
-rw-r--r--clang/unittests/Tooling/SourceCodeTest.cpp110
1 files changed, 108 insertions, 2 deletions
diff --git a/clang/unittests/Tooling/SourceCodeTest.cpp b/clang/unittests/Tooling/SourceCodeTest.cpp
index 258947a1a70..e3da9bf14b6 100644
--- a/clang/unittests/Tooling/SourceCodeTest.cpp
+++ b/clang/unittests/Tooling/SourceCodeTest.cpp
@@ -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<CallsVisitor> {
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
OpenPOWER on IntegriCloud