#include "FindBadConstructsAction.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "llvm/Support/TimeProfiler.h"
#include "FilteredASTConsumer.h"
#include "FindBadConstructsConsumer.h"
using namespace clang;
namespace {
const char kExcludeFieldsArgPrefix[] = "exclude-fields=";
}
namespace chrome_checker {
namespace {
class PluginConsumer : public FilteredASTConsumer {
public:
PluginConsumer(CompilerInstance* instance, const Options& options)
: visitor_(*instance, options) {}
void HandleTranslationUnit(clang::ASTContext& context) override {
llvm::TimeTraceScope TimeScope(
"HandleTranslationUnit for find-bad-constructs plugin");
ApplyFilter(context);
visitor_.Traverse(context);
}
private:
FindBadConstructsConsumer visitor_;
};
}
FindBadConstructsAction::FindBadConstructsAction() {
}
std::unique_ptr<ASTConsumer> FindBadConstructsAction::CreateASTConsumer(
CompilerInstance& instance,
llvm::StringRef ref) {
return std::make_unique<PluginConsumer>(&instance, options_);
}
bool FindBadConstructsAction::ParseArgs(const CompilerInstance& instance,
const std::vector<std::string>& args) {
for (llvm::StringRef arg : args) {
if (arg.starts_with(kExcludeFieldsArgPrefix)) {
options_.exclude_fields_file =
arg.substr(strlen(kExcludeFieldsArgPrefix)).str();
} else if (arg == "check-base-classes") {
options_.check_base_classes = true;
} else if (arg == "check-blink-data-member-type") {
options_.check_blink_data_member_type = true;
} else if (arg == "check-ipc") {
options_.check_ipc = true;
} else if (arg == "check-layout-object-methods") {
options_.check_layout_object_methods = true;
} else if (arg == "check-stack-allocated") {
options_.check_stack_allocated = true;
} else if (arg == "check-ptrs-to-non-string-literals") {
options_.check_ptrs_to_non_string_literals = true;
} else if (arg == "check-span-fields") {
options_.check_span_fields = true;
} else if (arg == "enable-match-profiling") {
options_.enable_match_profiling = true;
} else {
llvm::errs() << "Unknown clang plugin argument: " << arg << "\n";
return false;
}
}
return true;
}
}
static FrontendPluginRegistry::Add<chrome_checker::FindBadConstructsAction> X(
"find-bad-constructs",
"Finds bad C++ constructs");