|
| 1 | +#include "TypeCorrectCAPI.h" |
| 2 | +#include "TypeCorrectMain.h" |
| 3 | + |
| 4 | +#include <clang/Tooling/CompilationDatabase.h> |
| 5 | +#include <clang/Tooling/Refactoring.h> |
| 6 | +#include <clang/Tooling/Tooling.h> |
| 7 | +#include <iostream> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +namespace { |
| 11 | +class CAPIActionFactory : public clang::tooling::FrontendActionFactory { |
| 12 | + bool AuditMode; |
| 13 | + bool InPlace; |
| 14 | +public: |
| 15 | + CAPIActionFactory(bool AuditMode, bool InPlace) |
| 16 | + : AuditMode(AuditMode), InPlace(InPlace) {} |
| 17 | + |
| 18 | + std::unique_ptr<clang::FrontendAction> create() override { |
| 19 | + return std::make_unique<TypeCorrectPluginAction>( |
| 20 | + "", "", InPlace, false, AuditMode, |
| 21 | + type_correct::Phase::Standalone, "", ""); |
| 22 | + } |
| 23 | +}; |
| 24 | +} // namespace |
| 25 | + |
| 26 | +extern "C" TYPE_CORRECT_EXPORT int type_correct_audit(const char* target_path) { |
| 27 | + if (!target_path) return -1; |
| 28 | + |
| 29 | + std::vector<std::string> CommandLine; |
| 30 | + auto Compilations = std::make_unique<clang::tooling::FixedCompilationDatabase>(".", CommandLine); |
| 31 | + |
| 32 | + std::vector<std::string> SourcePaths = { target_path }; |
| 33 | + clang::tooling::RefactoringTool Tool(*Compilations, SourcePaths); |
| 34 | + |
| 35 | + CAPIActionFactory Factory(true /* AuditMode */, false /* InPlace */); |
| 36 | + return Tool.run(&Factory); |
| 37 | +} |
| 38 | + |
| 39 | +extern "C" TYPE_CORRECT_EXPORT int type_correct_fix(const char* target_path, bool dry_run) { |
| 40 | + if (!target_path) return -1; |
| 41 | + |
| 42 | + std::vector<std::string> CommandLine; |
| 43 | + auto Compilations = std::make_unique<clang::tooling::FixedCompilationDatabase>(".", CommandLine); |
| 44 | + |
| 45 | + std::vector<std::string> SourcePaths = { target_path }; |
| 46 | + clang::tooling::RefactoringTool Tool(*Compilations, SourcePaths); |
| 47 | + |
| 48 | + bool InPlace = !dry_run; |
| 49 | + CAPIActionFactory Factory(false /* AuditMode */, InPlace); |
| 50 | + |
| 51 | + if (dry_run) { |
| 52 | + return Tool.run(&Factory); |
| 53 | + } else { |
| 54 | + return Tool.runAndSave(&Factory); |
| 55 | + } |
| 56 | +} |
0 commit comments