[LibTooling] Extend `RewriteRule` with support for adding includes.
Summary:
This revision allows users to specify the insertion of an included directive (at
the top of the file being rewritten) as part of a rewrite rule. These
directives are bundled with `RewriteRule` cases, so that different cases can
potentially result in different include actions.
Reviewers: ilya-biryukov, gribozavr
Subscribers: cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D63892
llvm-svn: 364917
diff --git a/clang/unittests/Tooling/TransformerTest.cpp b/clang/unittests/Tooling/TransformerTest.cpp
index e9de00d..64f511b 100644
--- a/clang/unittests/Tooling/TransformerTest.cpp
+++ b/clang/unittests/Tooling/TransformerTest.cpp
@@ -198,6 +198,42 @@
testRule(std::move(Rule), Input, Expected);
}
+TEST_F(TransformerTest, AddIncludeQuoted) {
+ RewriteRule Rule = makeRule(callExpr(callee(functionDecl(hasName("f")))),
+ change(text("other()")));
+ addInclude(Rule, "clang/OtherLib.h");
+
+ std::string Input = R"cc(
+ int f(int x);
+ int h(int x) { return f(x); }
+ )cc";
+ std::string Expected = R"cc(#include "clang/OtherLib.h"
+
+ int f(int x);
+ int h(int x) { return other(); }
+ )cc";
+
+ testRule(Rule, Input, Expected);
+}
+
+TEST_F(TransformerTest, AddIncludeAngled) {
+ RewriteRule Rule = makeRule(callExpr(callee(functionDecl(hasName("f")))),
+ change(text("other()")));
+ addInclude(Rule, "clang/OtherLib.h", IncludeFormat::Angled);
+
+ std::string Input = R"cc(
+ int f(int x);
+ int h(int x) { return f(x); }
+ )cc";
+ std::string Expected = R"cc(#include <clang/OtherLib.h>
+
+ int f(int x);
+ int h(int x) { return other(); }
+ )cc";
+
+ testRule(Rule, Input, Expected);
+}
+
TEST_F(TransformerTest, NodePartNameNamedDecl) {
StringRef Fun = "fun";
RewriteRule Rule = makeRule(functionDecl(hasName("bad")).bind(Fun),