From c5a34e0b9e3806d5a65670060b15bd17d37b72d2 Mon Sep 17 00:00:00 2001 From: ykiko Date: Thu, 25 Jul 2024 19:57:06 +0800 Subject: [PATCH] add Directive. --- include/Clang/Clang.h | 3 +- include/Clang/Directive.h | 27 ++++++++++++++++ include/Clang/ParsedAST.h | 3 -- include/Clang/Preamble.h | 13 ++++---- src/Clang/Directive.cpp | 67 +++++++++++++++++++++++++++++++++++++++ src/Clang/ParsedAST.cpp | 56 ++------------------------------ 6 files changed, 103 insertions(+), 66 deletions(-) create mode 100644 include/Clang/Directive.h create mode 100644 src/Clang/Directive.cpp diff --git a/include/Clang/Clang.h b/include/Clang/Clang.h index 56974ed3..a806672c 100644 --- a/include/Clang/Clang.h +++ b/include/Clang/Clang.h @@ -9,11 +9,10 @@ #include #include - namespace clice { using llvm::StringRef; - +using PathRef = StringRef; using clang::CompilerInstance; using clang::CompilerInvocation; diff --git a/include/Clang/Directive.h b/include/Clang/Directive.h new file mode 100644 index 00000000..331217eb --- /dev/null +++ b/include/Clang/Directive.h @@ -0,0 +1,27 @@ +#include + +namespace clice { + +struct MacroDefinition { + std::string name; + std::string definition; + clang::SourceRange range; +}; + +struct MacroExpansion { + std::string name; + std::string expansion; +}; + +class Directive { +private: + std::string name; + clang::SourceRange range; + std::vector definitions; + std::vector expansions; + +public: + static std::unique_ptr create(clang::SourceManager& sourceManager, clang::SourceRange range); +}; + +} // namespace clice diff --git a/include/Clang/ParsedAST.h b/include/Clang/ParsedAST.h index a2171cd6..8dcfe477 100644 --- a/include/Clang/ParsedAST.h +++ b/include/Clang/ParsedAST.h @@ -9,7 +9,6 @@ namespace clice { class ParsedAST { private: using Decl = clang::Decl*; - using PathRef = llvm::StringRef; using TokenBuffer = clang::syntax::TokenBuffer; using ASTConsumer = std::unique_ptr; @@ -32,8 +31,6 @@ private: clang::SyntaxOnlyAction action; CompilerInstance instance; - ParsedAST() = default; - public: static std::unique_ptr build(std::string_view path, std::string_view content); diff --git a/include/Clang/Preamble.h b/include/Clang/Preamble.h index bdfda28a..6ef7af19 100644 --- a/include/Clang/Preamble.h +++ b/include/Clang/Preamble.h @@ -1,20 +1,19 @@ #pragma once -#include "Clang.h" +#include "Directive.h" namespace clice { class Preamble { private: - clang::PrecompiledPreamble* preamble; + Directive directive; + clang::PrecompiledPreamble data; - Preamble(clang::PrecompiledPreamble* preamble) : preamble(preamble) {} + Preamble(clang::PrecompiledPreamble&& preamble) : data(std::move(preamble)) {} public: - Preamble() = default; - - static Preamble - build(std::string_view path, std::string_view content, const CompilerInvocation& invocation); + static std::unique_ptr + build(PathRef path, StringRef content, const CompilerInvocation& invocation); }; } // namespace clice diff --git a/src/Clang/Directive.cpp b/src/Clang/Directive.cpp new file mode 100644 index 00000000..bc974434 --- /dev/null +++ b/src/Clang/Directive.cpp @@ -0,0 +1,67 @@ +#include + +namespace clice { + +class DirectiveCollector : clang::PPCallbacks { +public: + virtual ~DirectiveCollector() = default; + + void InclusionDirective(clang::SourceLocation HashLoc, + const clang::Token& IncludeTok, + StringRef FileName, + bool IsAngled, + clang::CharSourceRange FilenameRange, + clang::OptionalFileEntryRef File, + StringRef SearchPath, + StringRef RelativePath, + const clang::Module* Imported, + clang::SrcMgr::CharacteristicKind FileType) override {} + + void moduleImport(clang::SourceLocation ImportLoc, + clang::ModuleIdPath Path, + const clang::Module* Imported) override {} + + void PragmaDirective(clang::SourceLocation Loc, clang::PragmaIntroducerKind Introducer) override {} + + void MacroExpands(const clang::Token& MacroNameTok, + const clang::MacroDefinition& MD, + clang::SourceRange Range, + const clang::MacroArgs* Args) override {} + + void MacroDefined(const clang::Token& MacroNameTok, const clang::MacroDirective* MD) override {} + + void MacroUndefined(const clang::Token& MacroNameTok, + const clang::MacroDefinition& MD, + const clang::MacroDirective* Undef) override {} + + void If(clang::SourceLocation Loc, + clang::SourceRange ConditionRange, + ConditionValueKind ConditionValue) override {} + + void Elif(clang::SourceLocation Loc, + clang::SourceRange ConditionRange, + ConditionValueKind ConditionValue, + clang::SourceLocation IfLoc) override {} + + void Ifdef(clang::SourceLocation Loc, + const clang::Token& MacroNameTok, + const clang::MacroDefinition& MD) override {} + + void Elifdef(clang::SourceLocation Loc, + const clang::Token& MacroNameTok, + const clang::MacroDefinition& MD) override {} + + void Ifndef(clang::SourceLocation Loc, + const clang::Token& MacroNameTok, + const clang::MacroDefinition& MD) override {} + + void Elifndef(clang::SourceLocation Loc, + const clang::Token& MacroNameTok, + const clang::MacroDefinition& MD) override {} + + void Else(clang::SourceLocation Loc, clang::SourceLocation IfLoc) override {} + + void Endif(clang::SourceLocation Loc, clang::SourceLocation IfLoc) override {} +}; + +} // namespace clice diff --git a/src/Clang/ParsedAST.cpp b/src/Clang/ParsedAST.cpp index 0f11a5c2..8b7c2274 100644 --- a/src/Clang/ParsedAST.cpp +++ b/src/Clang/ParsedAST.cpp @@ -3,62 +3,10 @@ namespace clice { -std::unique_ptr ParsedAST::build(std::string_view path, std::string_view content) { - auto command = CompileDatabase::instance().lookup(path); - std::shared_ptr invocation = clang::createInvocation(command); - - auto& inputs = invocation->getFrontendOpts().Inputs; - inputs.push_back(clang::FrontendInputFile(path, clang::InputKind{clang::Language::CXX})); - - auto preamble = Preamble::build(path, content, *invocation); - - return build(path, invocation, preamble); -} +std::unique_ptr ParsedAST::build(std::string_view path, std::string_view content) {} std::unique_ptr ParsedAST::build(std::string_view path, const std::shared_ptr& invocation, - const Preamble& preamble) { - auto AST = new ParsedAST(); - AST->path = path; - - // some settings for CompilerInstance - auto& instance = AST->instance; - instance.setInvocation(invocation); - instance.createDiagnostics(); - - if(!instance.createTarget()) { - logger::error("Failed to create target"); - } - - if(auto manager = instance.createFileManager()) { - instance.createSourceManager(*manager); - } else { - logger::error("Failed to create file manager"); - } - - instance.createPreprocessor(clang::TranslationUnitKind::TU_Complete); - - instance.createASTContext(); - - // start FrontendAction - const auto& input = instance.getFrontendOpts().Inputs[0]; - auto& action = AST->action; - - if(!action.BeginSourceFile(instance, input)) { - logger::error("Failed to begin source file"); - } - - clang::syntax::TokenCollector collector = {instance.getPreprocessor()}; - - if(llvm::Error error = action.Execute()) { - logger::error("Failed to execute action: {0}", llvm::errorToErrorCode(std::move(error)).message()); - } - - - - // AST->tokens.construct(std::move(collector).consume()); - - return std::unique_ptr(AST); -} + const Preamble& preamble) {} } // namespace clice