From 91bc14c129f21c2ba3ca9745fd8f0b6916e69768 Mon Sep 17 00:00:00 2001 From: ykiko Date: Sat, 31 Aug 2024 20:56:19 +0800 Subject: [PATCH] update Diagnostic. --- CMakeLists.txt | 2 ++ include/AST/Diagnostic.h | 15 ++++++++++++++- src/AST/Diagnostic.cpp | 20 ++++++++++++++++++++ src/AST/ParsedAST.cpp | 6 +----- src/AST/Preamble.cpp | 5 +---- 5 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 src/AST/Diagnostic.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 17714025..11448dd2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,8 @@ add_executable(clice) file(GLOB_RECURSE SRC_FILES "${CMAKE_SOURCE_DIR}/src/*.cpp") target_sources(clice PRIVATE ${SRC_FILES}) +target_compile_options(clice PRIVATE -fno-rtti) + target_include_directories(clice PRIVATE ${CMAKE_SOURCE_DIR}/include ${LLVM_INCLUDE_DIRS} diff --git a/include/AST/Diagnostic.h b/include/AST/Diagnostic.h index 9a2f261c..8122e453 100644 --- a/include/AST/Diagnostic.h +++ b/include/AST/Diagnostic.h @@ -8,4 +8,17 @@ #include #include #include -#include \ No newline at end of file +#include + +namespace clice { + +class Diagnostic : public clang::DiagnosticConsumer { +public: + void BeginSourceFile(const clang::LangOptions& Opts, const clang::Preprocessor* PP) override; + + void HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic& Info) override; + + void EndSourceFile() override; +}; + +} // namespace clice diff --git a/src/AST/Diagnostic.cpp b/src/AST/Diagnostic.cpp new file mode 100644 index 00000000..38adf785 --- /dev/null +++ b/src/AST/Diagnostic.cpp @@ -0,0 +1,20 @@ +#include "AST/Diagnostic.h" + +namespace clice { + +void Diagnostic::BeginSourceFile(const clang::LangOptions& Opts, const clang::Preprocessor* PP) { + +}; + +void Diagnostic::HandleDiagnostic(clang::DiagnosticsEngine::Level level, const clang::Diagnostic& diagnostic) { + llvm::SmallString<128> message; + diagnostic.FormatDiagnostic(message); + // diagnostic.getLocation(); + llvm::outs() << "Diagnostic: " << message << "\n"; +}; + +void Diagnostic::EndSourceFile() { + +}; + +} // namespace clice diff --git a/src/AST/ParsedAST.cpp b/src/AST/ParsedAST.cpp index 830e08a5..a157aee3 100644 --- a/src/AST/ParsedAST.cpp +++ b/src/AST/ParsedAST.cpp @@ -34,11 +34,7 @@ std::unique_ptr ParsedAST::build(llvm::StringRef filename, auto instance = std::make_unique(); instance->setInvocation(std::move(invocation)); - clang::DiagnosticIDs* ids = new clang::DiagnosticIDs(); - clang::DiagnosticOptions* diag_opts = new clang::DiagnosticOptions(); - clang::DiagnosticConsumer* consumer = new clang::TextDiagnosticPrinter(llvm::errs(), diag_opts); - clang::DiagnosticsEngine* engine = new clang::DiagnosticsEngine(ids, diag_opts, consumer); - instance->setDiagnostics(engine); + instance->createDiagnostics(new Diagnostic(), true); // if(auto remappingVSF = // createVFSFromCompilerInvocation(instance->getInvocation(), instance->getDiagnostics(), vfs)) { diff --git a/src/AST/Preamble.cpp b/src/AST/Preamble.cpp index c0437f2d..94325538 100644 --- a/src/AST/Preamble.cpp +++ b/src/AST/Preamble.cpp @@ -12,10 +12,7 @@ std::unique_ptr auto bounds = clang::ComputePreambleBounds(invocation->getLangOpts(), *buffer, false); // create diagnostic engine - clang::DiagnosticsEngine* engine = - new clang::DiagnosticsEngine(new clang::DiagnosticIDs(), - new clang::DiagnosticOptions(), - new clang::TextDiagnosticPrinter(llvm::errs(), new clang::DiagnosticOptions())); + auto engine = clang::CompilerInstance::createDiagnostics(&invocation->getDiagnosticOpts(), new Diagnostic()); // if store the preamble in memory, if not, store it in a file(storagePath) bool storeInMemory = false;