-
Notifications
You must be signed in to change notification settings - Fork 1
/
force_cover.cpp
150 lines (119 loc) · 5.16 KB
/
force_cover.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//------------------------------------------------------------------------------
// Force test coverage in C++
//
// This code modifies C++ source code to force test coverage
//
//
// Adapted from AST matching sample by
// Eli Bendersky (eliben@gmail.com)
//------------------------------------------------------------------------------
#include <string>
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/raw_ostream.h"
#include "clang/AST/Decl.h"
#include <iostream>
#include <set>
#include <fstream>
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::driver;
using namespace clang::tooling;
static llvm::cl::OptionCategory MatcherSampleCategory("Matcher Sample");
class TemplateClassHandler : public MatchFinder::MatchCallback {
public:
TemplateClassHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {}
virtual void run(const MatchFinder::MatchResult &Result) {
const CXXMethodDecl *ClassNode = Result.Nodes.getNodeAs<CXXMethodDecl>("class");
if (ClassNode->isImplicit() || !ClassNode->getLocation().isValid() || ClassNode->isFromASTFile() || !ClassNode->isThisDeclarationADefinition()) {
return;
}
std::string source_range = clang::Lexer::getSourceText(clang::CharSourceRange::getCharRange(ClassNode->getSourceRange()), Rewrite.getSourceMgr(), Rewrite.getLangOpts()).str();
std::size_t loc = source_range.find_first_of("{");
if (loc == std::string::npos) {
loc = 0;
} else {
loc++;
}
Rewrite.InsertText(ClassNode->getBeginLoc().getLocWithOffset(loc), "/*_FORCE_COVER_START_*/", true, true);
Rewrite.InsertText(ClassNode->getEndLoc().getLocWithOffset(1), "/*_FORCE_COVER_END_*/", true, true);
}
private:
Rewriter &Rewrite;
};
class TemplateFunctionHandler : public MatchFinder::MatchCallback {
public:
TemplateFunctionHandler(Rewriter &Rewrite) : Rewrite(Rewrite) {}
virtual void run(const MatchFinder::MatchResult &Result) {
const FunctionTemplateDecl *FunctionNode = Result.Nodes.getNodeAs<FunctionTemplateDecl>("fun");
if (FunctionNode->isImplicit() || !FunctionNode->getLocation().isValid() || !FunctionNode->isThisDeclarationADefinition()) {
return;
}
std::string source_range = clang::Lexer::getSourceText(clang::CharSourceRange::getCharRange(FunctionNode->getSourceRange()), Rewrite.getSourceMgr(), Rewrite.getLangOpts()).str();
std::size_t loc = source_range.find_first_of("{");
if (loc == std::string::npos) {
loc = 0;
} else {
loc++;
}
Rewrite.InsertText(FunctionNode->getBeginLoc().getLocWithOffset(loc), "/*_FORCE_COVER_START_*/", true, true);
Rewrite.InsertText(FunctionNode->getEndLoc().getLocWithOffset(1), "/*_FORCE_COVER_END_*/", true, true);
}
private:
Rewriter &Rewrite;
};
// Implementation of the ASTConsumer interface for reading an AST produced
// by the Clang parser. It registers a couple of matchers and runs them on
// the AST.
class MyASTConsumer : public ASTConsumer {
public:
MyASTConsumer(Rewriter &R) : HandlerForTemplateClass(R), HandlerForTemplateFunction(R) {
// Match methods of templated classes
Matcher.addMatcher(cxxMethodDecl(anyOf(hasAncestor(classTemplateDecl()), hasAncestor(classTemplateSpecializationDecl()))).bind("class"), &HandlerForTemplateClass);
// Match templated functions
Matcher.addMatcher(functionTemplateDecl().bind("fun"), &HandlerForTemplateFunction);
}
void HandleTranslationUnit(ASTContext &Context) override {
// Run the matchers when we have the whole TU parsed.
Matcher.matchAST(Context);
}
private:
TemplateClassHandler HandlerForTemplateClass;
TemplateFunctionHandler HandlerForTemplateFunction;
MatchFinder Matcher;
};
// For each source file provided to the tool, a new FrontendAction is created.
class MyFrontendAction : public ASTFrontendAction {
public:
MyFrontendAction() {}
void EndSourceFileAction() override {
TheRewriter.getEditBuffer(TheRewriter.getSourceMgr().getMainFileID())
.write(llvm::outs());
}
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef file) override {
TheRewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts());
return std::make_unique<MyASTConsumer>(TheRewriter);
}
private:
Rewriter TheRewriter;
};
int main(int argc, const char **argv) {
// From https://github.com/llvm/llvm-project/blob/e59f648d698efe58b96e9b6224449b2b8cfa872a/clang/docs/LibASTMatchersTutorial.rst
auto ExpectedParser = CommonOptionsParser::create(argc, argv, MatcherSampleCategory);
if (!ExpectedParser) {
// Fail gracefully for unsupported options.
llvm::errs() << ExpectedParser.takeError();
return 1;
}
CommonOptionsParser& op = ExpectedParser.get();
ClangTool Tool(op.getCompilations(), op.getSourcePathList());
return Tool.run(newFrontendActionFactory<MyFrontendAction>().get());
}