TEST(ClangToolTest, ArgumentAdjusters) { FixedCompilationDatabase Compilations("/", std::vector<std::string>()); ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc")); Tool.mapVirtualFile("/a.cc", "void a() {}"); std::unique_ptr<FrontendActionFactory> Action( newFrontendActionFactory<SyntaxOnlyAction>()); bool Found = false; bool Ran = false; ArgumentsAdjuster CheckSyntaxOnlyAdjuster = [&Found, &Ran](const CommandLineArguments &Args) { Ran = true; if (std::find(Args.begin(), Args.end(), "-fsyntax-only") != Args.end()) Found = true; return Args; }; Tool.appendArgumentsAdjuster(CheckSyntaxOnlyAdjuster); Tool.run(Action.get()); EXPECT_TRUE(Ran); EXPECT_TRUE(Found); Ran = Found = false; Tool.clearArgumentsAdjusters(); Tool.appendArgumentsAdjuster(CheckSyntaxOnlyAdjuster); Tool.appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster()); Tool.run(Action.get()); EXPECT_TRUE(Ran); EXPECT_FALSE(Found); }
ClangTool::ClangTool(const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths, std::shared_ptr<PCHContainerOperations> PCHContainerOps) : Compilations(Compilations), SourcePaths(SourcePaths), PCHContainerOps(PCHContainerOps), Files(new FileManager(FileSystemOptions())), DiagConsumer(nullptr) { appendArgumentsAdjuster(getClangStripOutputAdjuster()); appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster()); }
ClangTool::ClangTool(const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths, std::shared_ptr<PCHContainerOperations> PCHContainerOps) : Compilations(Compilations), SourcePaths(SourcePaths), PCHContainerOps(PCHContainerOps), OverlayFileSystem(new vfs::OverlayFileSystem(vfs::getRealFileSystem())), InMemoryFileSystem(new vfs::InMemoryFileSystem), Files(new FileManager(FileSystemOptions(), OverlayFileSystem)), DiagConsumer(nullptr) { OverlayFileSystem->pushOverlay(InMemoryFileSystem); appendArgumentsAdjuster(getClangStripOutputAdjuster()); appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster()); }
// 开始分析 void Run(const CxxCleanOptionsParser &optionParser) { ClangTool tool(optionParser.getCompilations(), Project::instance.m_cpps); tool.clearArgumentsAdjusters(); tool.appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster()); optionParser.AddClangArgumentByOption(tool); optionParser.AddClangArgument(tool, "-fcxx-exceptions"); optionParser.AddClangArgument(tool, "-nobuiltininc"); // 禁止使用clang内置的头文件 optionParser.AddClangArgument(tool, "-w"); // 禁用警告 optionParser.AddClangArgument(tool, "-Wno-everything"); // 禁用任何警告,比-w级别高 optionParser.AddClangArgument(tool, "-ferror-limit=5"); // 限制单个cpp产生的编译错误数,超过则不再编译 optionParser.AddClangArgument(tool, "-fpermissive"); // 对不某些不符合标准的行为,允许编译通过,即对标准做降级处理 DiagnosticOptions diagnosticOptions; diagnosticOptions.ShowOptionNames = 1; tool.setDiagnosticConsumer(new CxxcleanDiagnosticConsumer(&diagnosticOptions)); // 注意:这里用new没关系,会被释放 // 对每个文件进行语法分析 tool.run(newFrontendActionFactory<CxxCleanAction>().get()); ProjectHistory::instance.Print(); HtmlLog::instance.Close(); }