int UseAutoTransform::apply(const clang::tooling::CompilationDatabase &Database, const std::vector<std::string> &SourcePaths) { ClangTool UseAutoTool(Database, SourcePaths, SharedModuleProvider::Create<LLVMModuleProvider>()); unsigned AcceptedChanges = 0; MatchFinder Finder; ReplacementsVec Replaces; IteratorReplacer ReplaceIterators(AcceptedChanges, Options().MaxRiskLevel, /*Owner=*/ *this); NewReplacer ReplaceNew(AcceptedChanges, Options().MaxRiskLevel, /*Owner=*/ *this); Finder.addMatcher(makeIteratorDeclMatcher(), &ReplaceIterators); Finder.addMatcher(makeDeclWithNewMatcher(), &ReplaceNew); if (int Result = UseAutoTool.run(createActionFactory(Finder).get())) { llvm::errs() << "Error encountered during translation.\n"; return Result; } setAcceptedChanges(AcceptedChanges); return 0; }
int QueryMangledNameTransform::apply(const CompilationDatabase &Database, const std::vector<std::string> &SourcePaths, const llvm::cl::list<std::string>& LineRanges ) { llvm::errs() << "QueryMangledName begin\n"; parsePositionArguments( LineRanges ); ClangTool QueryMangledNameTool(Database, SourcePaths); unsigned AcceptedChanges = 0; MatchFinder Finder; QueryMangledNameFixer Fixer(AcceptedChanges, /*Owner=*/ *this); Finder.addMatcher(makeQueryMangledNameMatcher(), &Fixer); Finder.addMatcher(makeQueryMangledNameMatcherRef(), &Fixer); if (int result = QueryMangledNameTool.run(createActionFactory(Finder))) { llvm::errs() << "Error encountered during translation.\n"; return result; } setAcceptedChanges(AcceptedChanges); llvm::errs() << "QueryMangledName done\n"; return 0; }
int main(int argc, const char** argv) { // Parse the arguments and pass them to the the internal sub-tools CommonOptionsParser optionsParser(argc, argv, annotatorCategory); PathFilter bindingPathFilter (optionsParser.getSourcePathList(), [](const std::string& path) { return path.find("API.cpp") != std::string::npos; }); PathFilter nonBindingPathFilter (optionsParser.getSourcePathList(), [](const std::string& path) { return path.find("API.cpp") == std::string::npos; }); ClangTool bindingExtractor(optionsParser.getCompilations(), bindingPathFilter.GetPathList()); RefactoringTool annotator(optionsParser.getCompilations(), nonBindingPathFilter.GetPathList()); // Setup finder to match against AST nodes from existing AngelScript binding source files ExtractCallback extractCallback; MatchFinder bindingFinder; // Find exposed class names (they are registered through RegisterObjectType(), RegisterRefCounted(), RegisterObject(), etc) bindingFinder.addMatcher( memberCallExpr( callee( methodDecl(hasName("RegisterObjectType"))), hasArgument(0, stringLiteral().bind("className"))), &extractCallback); bindingFinder.addMatcher( callExpr( hasDeclaration( functionDecl(hasParameter(1, hasName("className")))), hasArgument(1, stringLiteral().bind("className"))), &extractCallback); // Setup finder to match against AST nodes for annotating Urho3D library source files AnnotateCallback annotateCallback(annotator.getReplacements()); MatchFinder annotateFinder; // Find exported class declaration with Urho3D namespace annotateFinder.addMatcher( recordDecl( #ifndef _MSC_VER hasAttr(attr::Visibility), #else hasAttr(attr::DLLExport), #endif matchesName("^::Urho3D::")).bind("className"), &annotateCallback); // Unbuffered stdout stream to keep the Travis-CI's log flowing and thus prevent it from killing a potentially long running job outs().SetUnbuffered(); // Success when both sub-tools are run successfully return (outs() << "Extracting", true) && bindingExtractor.run(newFrontendActionFactory(&bindingFinder).get()) == EXIT_SUCCESS && (outs() << "\nExtracted " << classNames_.size() << " bound class names\n" << "Annotating", true) && annotator.runAndSave(newFrontendActionFactory(&annotateFinder).get()) == EXIT_SUCCESS && (outs() << "\nAnnotated " << annotatedClassNames_.size() << " exported class names as non-scriptable\n", true) ? EXIT_SUCCESS : EXIT_FAILURE; }
void VariableNamingRule::RegisterASTMatcherCallback(MatchFinder& finder) { finder.addMatcher( varDecl(unless(anyOf(isExpansionInSystemHeader(), // ignore system headers isImplicit(), // ignore implicit (compiler-generated) variables hasEmptyName()))) // unnamed function parameters are fine .bind("varDecl"), this); finder.addMatcher( fieldDecl(unless(anyOf(isExpansionInSystemHeader(), isImplicit(), hasEmptyName()))) .bind("fieldDecl"), this); }
void CanRunScriptChecker::buildFuncSet(ASTContext *Context) { // We create a match finder. MatchFinder Finder; // We create the callback which will be called when we find a function with // a MOZ_CAN_RUN_SCRIPT annotation. FuncSetCallback Callback(CanRunScriptFuncs); // We add the matcher to the finder, linking it to our callback. Finder.addMatcher( functionDecl(hasCanRunScriptAnnotation()).bind("canRunScriptFunction"), &Callback); Finder.addMatcher( lambdaExpr().bind("lambda"), &Callback); // We start the analysis, given the ASTContext our main checker is in. Finder.matchAST(*Context); }
int main(int argc, const char **argv) { CommonOptionsParser op(argc, argv, ToolingSampleCategory); RefactoringTool Tool(op.getCompilations(), op.getSourcePathList()); // Set up AST matcher callbacks. IfStmtHandler HandlerForIf(&Tool.getReplacements()); MatchFinder Finder; Finder.addMatcher(ifStmt().bind("ifStmt"), &HandlerForIf); // Run the tool and collect a list of replacements. We could call runAndSave, // which would destructively overwrite the files with their new contents. // However, for demonstration purposes it's interesting to show the // replacements. if (int Result = Tool.run(newFrontendActionFactory(&Finder).get())) { return Result; } llvm::outs() << "Replacements collected by the tool:\n"; for (auto &r : Tool.getReplacements()) { llvm::outs() << r.toString() << "\n"; } return 0; }
//Main @TestNeed int main(int argc, const char **argv){ CommonOptionsParser OptionsParser(argc, argv, MyToolCategory); ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList()); CallExprChecker cec; MatchFinder finder; finder.addMatcher(mallocMatcher, &cec); finder.addMatcher(freeMatcher, &cec); finder.addMatcher(reallocMatcher, &cec); finder.addMatcher(memcpyAryMatcher, &cec); finder.addMatcher(memcpyPtrMatcher, &cec); finder.addMatcher(memcmpAryMatcher, &cec); finder.addMatcher(memcmpPtrMatcher, &cec); Tool.run(newFrontendActionFactory(&finder)); return 0; }
/* * Main @TestNeed */ int main(int argc, const char **argv){ CommonOptionsParser OptionsParser(argc, argv, MyToolCategory); //Create an ClangTool instance to run a FrontendAction over a set of files ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList()); //tool::MyFactory Factory; SimilarNameChecker snc; MatchFinder finder; finder.addMatcher(VarNameMatcher, &snc); Tool.run(newFrontendActionFactory(&finder)); for(int i = 0; i < nameVec.size(); ++i) { std::string oneName(nameVec[i]); for(int j = i + 1; j < nameVec.size(); ++j) { std::string anotherName(nameVec[j]); if(calculateSimilarity(oneName,anotherName) >= 0.5 && calculateSimilarity(oneName,anotherName) != 1) { ofstream out("Rule053", std::ostream::app); out << oneName << " : " << anotherName << "\n"; } } } return 0; }
TEST(StmtDepthMetricTest, ObjCForCollectionStatement) { StmtDepthCallback depthCallback(2); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &depthCallback); testMatcherOnObjCCode(finder, "void m() { id array; for(id one in array) {} }"); }
TEST(StmtDepthMetricTest, ChooseTheDeepest) { StmtDepthCallback depthCallback(4); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &depthCallback); testMatcherOnCode(finder, "void m() {{}{{{}}}{{}}}"); }
TEST(StmtDepthMetricTest, IfStatementWithElse) { StmtDepthCallback depthCallback(4); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &depthCallback); testMatcherOnCode(finder, "void m() { if (1) {} else {{{}}} }"); }
TEST(StmtDepthMetricTest, EmptyFunction) { StmtDepthCallback depthCallback(1); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &depthCallback); testMatcherOnCode(finder, "void m() {}"); }
TEST(StmtDepthMetricTest, TwoLevelBlock) { StmtDepthCallback depthCallback(2); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &depthCallback); testMatcherOnCode(finder, "void m() {{}}"); }
TEST(NcssMetricTest, DeclStmtWithManyVariables) { NCSSCallback ncssCallback(1); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &ncssCallback); testMatcherOnCode(finder, "void m() { int a = 1, b = 2, c = 3; }"); }
TEST(StmtDepthMetricTest, TwoCaseStatementsAndDefault) { StmtDepthCallback depthCallback(2); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &depthCallback); testMatcherOnCode(finder, "void m() { int i = 1; switch (i) { case 1: i = 2; break; case 2: break; default: break; } }"); }
TEST(NcssMetricTest, ObjCAutoreleasePool) { NCSSCallback ncssCallback(2); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &ncssCallback); testMatcherOnObjCCode(finder, "void m() { @autoreleasepool { int a = 1; } }"); }
TEST(NcssMetricTest, NullStmtsDoNotContribute) { NCSSCallback ncssCallback(0); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &ncssCallback); testMatcherOnCode(finder, "void m() {\n;\n;\n;\n;\n;}"); }
TEST(StmtDepthMetricTest, CXXTryCatch) { StmtDepthCallback depthCallback(4); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &depthCallback); testMatcherOnCXXCode(finder, "void m() { try { int a = 1; } catch (...) { int b = 2; {{}}} }"); }
TEST(StmtDepthMetricTest, ObjCTryCatchFinally) { StmtDepthCallback depthCallback(4); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &depthCallback); testMatcherOnObjCCode(finder, "void m() { @try { int c = 3; } @catch (id ex) { int a = 1; } @finally { int b = 2; {{}}} }"); }
TEST(NcssMetricTest, IfStatementWithElse) { NCSSCallback ncssCallback(2); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &ncssCallback); testMatcherOnCode(finder, "void m() { if (1) {} else {} }"); }
TEST(NcssMetricTest, EmptyCompoundStmt) { NCSSCallback ncssCallback(0); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &ncssCallback); testMatcherOnCode(finder, "void m() {\n{}\n{}\n{}\n{}\n{}}"); }
TEST(NcssMetricTest, MethodCall) { NCSSCallback ncssCallback(1); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &ncssCallback); testMatcherOnCode(finder, "void n(); void m() { n(); }"); }
TEST(StmtDepthMetricTest, WhileStatement) { StmtDepthCallback depthCallback(2); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &depthCallback); testMatcherOnCode(finder, "void m() { while(1) {} }"); }
TEST(NcssMetricTest, OneCaseStatement) { NCSSCallback ncssCallback(4); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &ncssCallback); testMatcherOnCode(finder, "void m() { int i = 1; switch (i) { case 1: break; } }"); }
TEST(NcssMetricTest, TwoCaseStatementsAndDefault) { NCSSCallback ncssCallback(9); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &ncssCallback); testMatcherOnCode(finder, "void m() { int i = 1; switch (i) { case 1: i = 2; break; case 2: break; default: break; } }"); }
TEST(NcssMetricTest, SemiColonAfterDeclStmtCount) { NCSSCallback ncssCallback(1); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &ncssCallback); testMatcherOnCode(finder, "void m() { int a = 1; }"); }
TEST(StmtDepthMetricTest, ObjCSynchronized) { StmtDepthCallback depthCallback(2); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &depthCallback); testMatcherOnObjCCode(finder, "void m() { id res; @synchronized(res) { int a = 1; } }"); }
TEST(StmtDepthMetricTest, OneCaseStatement) { StmtDepthCallback depthCallback(2); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &depthCallback); testMatcherOnCode(finder, "void m() { int i = 1; switch (i) { case 1: break; } }"); }
TEST(StmtDepthMetricTest, ObjCAutoreleasePool) { StmtDepthCallback depthCallback(2); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &depthCallback); testMatcherOnObjCCode(finder, "void m() { @autoreleasepool { int a = 1; } }"); }
TEST(NcssMetricTest, ObjCSynchronized) { NCSSCallback ncssCallback(3); MatchFinder finder; finder.addMatcher(functionDeclMatcher, &ncssCallback); testMatcherOnObjCCode(finder, "void m() { id res; @synchronized(res) { int a = 1; } }"); }