Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .clangd

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/publish-clice.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:

- uses: ./.github/actions/setup-pixi
with:
environments: develop
environments: package

- name: Remove ci llvm toolchain on Windows
if: runner.os == 'Windows'
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/test-cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ jobs:
uses: actions/checkout@v4

- uses: ./.github/actions/setup-pixi
with:
environments: develop

- name: Build
run: pixi run build ${{ matrix.build_type }} ON
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/test-xmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ jobs:
build-cache-key: ${{ matrix.os }}-${{ matrix.build_type }}

- uses: ./.github/actions/setup-pixi
with:
environments: develop

- name: Build
run: pixi run xmake ${{ matrix.build_type }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ tests/unit/Local/
.vs/
.idea/
.claude
.clangd

# pixi environments
.env
Expand Down
2 changes: 1 addition & 1 deletion docs/en/dev/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pixi run integration-test Debug
```

> [!TIP]
> If you want to develop directly with `cmake`, `ninja`, `clang++`, etc., run `pixi shell -e develop` to enter a shell with all env vars configured.
> If you want to develop directly with `cmake`, `ninja`, `clang++`, etc., run `pixi shell` to enter a shell with all env vars configured.

### XMake

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/dev/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pixi run integration-test Debug
```

> [!TIP]
> 如果你想直接使用 `cmake`, `ninja`, `clang++` 等命令进行开发,请运行 `pixi shell -e develop` 进入已配置好环境变量的终端
> 如果你想直接使用 `cmake`, `ninja`, `clang++` 等命令进行开发,请运行 `pixi shell` 进入已配置好环境变量的终端

### XMake

Expand Down
4 changes: 2 additions & 2 deletions include/AST/FilterASTVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class FilteredASTVisitor : public clang::RecursiveASTVisitor<Derived> {
public:
using Base = clang::RecursiveASTVisitor<Derived>;

FilteredASTVisitor(CompilationUnit& unit, bool interested_only) :
FilteredASTVisitor(CompilationUnitRef unit, bool interested_only) :
unit(unit), interested_only(interested_only) {}

#define CHECK_DERIVED_IMPL(func) \
Expand Down Expand Up @@ -181,7 +181,7 @@ class FilteredASTVisitor : public clang::RecursiveASTVisitor<Derived> {
#undef CHECK_DERIVED_IMPL

protected:
CompilationUnit& unit;
CompilationUnitRef unit;
bool interested_only;
};

Expand Down
8 changes: 4 additions & 4 deletions include/AST/Selection.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace clice {

class CompilationUnit;
class CompilationUnitRef;

/// A selection can partially or completely cover several AST nodes.
/// The SelectionTree contains nodes that are covered, and their parents.
Expand Down Expand Up @@ -47,15 +47,15 @@ class SelectionTree {
/// - Func should return true on success (stop) and false on failure (continue)
///
/// Always yields at least one tree. If no tokens are touched, it is empty.
static bool create_each(CompilationUnit& unit,
static bool create_each(CompilationUnitRef unit,
LocalSourceRange range,
llvm::function_ref<bool(SelectionTree)> callback);

/// Create a selection tree for the given range.
///
/// Where ambiguous (range is empty and borders two tokens), prefer the token
/// on the right.
static SelectionTree create_right(CompilationUnit& unit, LocalSourceRange range);
static SelectionTree create_right(CompilationUnitRef unit, LocalSourceRange range);

/// Copies are no good - contain pointers to other nodes.
SelectionTree(const SelectionTree&) = delete;
Expand Down Expand Up @@ -139,7 +139,7 @@ class SelectionTree {
private:
// Creates a selection tree for the given range in the main file.
// The range includes bytes [Start, End).
SelectionTree(CompilationUnit& unit, LocalSourceRange range);
SelectionTree(CompilationUnitRef unit, LocalSourceRange range);

// Stable-pointer storage, FIXME: use memory pool instead?
std::deque<Node> nodes;
Expand Down
4 changes: 2 additions & 2 deletions include/AST/Semantic.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SemanticVisitor : public FilteredASTVisitor<SemanticVisitor<Derived>> {
public:
using Base = FilteredASTVisitor<SemanticVisitor>;

SemanticVisitor(CompilationUnit& unit, bool interested_only) :
SemanticVisitor(CompilationUnitRef unit, bool interested_only) :
Base(unit, interested_only), unit(unit), resolver(unit.resolver()) {}

public:
Expand Down Expand Up @@ -725,7 +725,7 @@ class SemanticVisitor : public FilteredASTVisitor<SemanticVisitor<Derived>> {
}

protected:
CompilationUnit& unit;
CompilationUnitRef unit;
TemplateResolver& resolver;
llvm::SmallVector<clang::Decl*> decls;
};
Expand Down
2 changes: 1 addition & 1 deletion include/AST/SourceCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ struct Token {
return is_at_start_of_line && kind == clang::tok::hash;
}

/// The tokens after the include diretive are regarded as
/// The tokens after the include directive are regarded as
/// a whole token, whose kind is `header_name`. For example
/// `<iostream>` and `"test.h"` are both header name.
bool is_header_name() const {
Expand Down
17 changes: 6 additions & 11 deletions include/Compiler/Compilation.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace clice {

struct CompilationParams {
/// The kind of this compilation.
CompilationUnit::Kind kind;
CompilationKind kind;

/// Whether to run clang-tidy.
bool clang_tidy = false;
Expand Down Expand Up @@ -48,9 +48,6 @@ struct CompilationParams {
/// to cancel old compilation task.
std::shared_ptr<std::atomic_bool> stop = std::make_shared<std::atomic_bool>(false);

/// Store all compilation errors in the process.
std::shared_ptr<std::vector<Diagnostic>> diagnostics;

void add_remapped_file(llvm::StringRef path,
llvm::StringRef content,
std::uint32_t bound = -1) {
Expand All @@ -62,23 +59,21 @@ struct CompilationParams {
}
};

using CompilationResult = std::expected<CompilationUnit, std::string>;

/// Only preprocess ths source flie.
CompilationResult preprocess(CompilationParams& params);
CompilationUnit preprocess(CompilationParams& params);

/// Build AST from given file path and content. If pch or pcm provided, apply them to the compiler.
/// Note this function will not check whether we need to update the PCH or PCM, caller should check
/// their reusability and update in time.
CompilationResult compile(CompilationParams& params);
CompilationUnit compile(CompilationParams& params);

/// Build PCH from given file path and content.
CompilationResult compile(CompilationParams& params, PCHInfo& out);
CompilationUnit compile(CompilationParams& params, PCHInfo& out);

/// Build PCM from given file path and content.
CompilationResult compile(CompilationParams& params, PCMInfo& out);
CompilationUnit compile(CompilationParams& params, PCMInfo& out);

/// Run code completion at the given location.
CompilationResult complete(CompilationParams& params, clang::CodeCompleteConsumer* consumer);
CompilationUnit complete(CompilationParams& params, clang::CodeCompleteConsumer* consumer);

} // namespace clice
102 changes: 70 additions & 32 deletions include/Compiler/CompilationUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,71 @@

namespace clice {

/// All AST related information needed for language server.
class CompilationUnit {
public:
/// The kind describes how we preprocess this source file
/// to get this compilation unit.
enum class Kind : std::uint8_t {
/// From preprocessing the source file. Therefore directives
/// are available but AST nodes are not.
Preprocess,
enum class CompilationKind : std::uint8_t {
/// From preprocessing the source file. Therefore directives
/// are available but AST nodes are not.
Preprocess,

/// From indexing the static source file.
Indexing,
/// From indexing the static source file.
Indexing,

/// From building preamble for the source file.
Preamble,
/// From building preamble for the source file.
Preamble,

/// From building precompiled module for the module interface unit.
ModuleInterface,
/// From building precompiled module for the module interface unit.
ModuleInterface,

/// From building normal AST for source file(except preamble), interested file and top level
/// declarations are available.
Content,
/// From building normal AST for source file(except preamble), interested file and top level
/// declarations are available.
Content,

/// From running code completion for the source file(preamble is applied).
Completion,
};
/// From running code completion for the source file(preamble is applied).
Completion,
};

using enum Kind;
struct Impl;
enum class CompilationStatus : std::uint8_t {
Completed,
Cancelled,
SetupFail,
FatalError,
};

CompilationUnit(Kind kind, Impl* impl) : kind(kind), impl(impl) {}
class CompilationUnitRef {
public:
struct Self;

CompilationUnit(const CompilationUnit&) = delete;
CompilationUnitRef(Self* self) : self(self) {}

CompilationUnit(CompilationUnit&& other) : kind(other.kind), impl(other.impl) {
other.impl = nullptr;
Self* operator->() {
return self;
}

~CompilationUnit();
public:
CompilationKind kind();

CompilationStatus status();

/// Parse finished; ASTContext is usable but diagnostics may still contain errors.
bool completed() {
return status() == CompilationStatus::Completed;
}

/// Compilation was cancelled; consumers should not touch any state.
bool cancelled() {
return status() == CompilationStatus::Cancelled;
}

/// Failed during initial setup; diagnostics exist (location-free), ASTContext
/// is unavailable.
bool setup_fail() {
return status() == CompilationStatus::SetupFail;
}

/// Hit an unrecoverable error; diagnostics and decoded source locations
/// are usable, other states are not unavailable.
bool fatal_error() {
return status() == CompilationStatus::FatalError;
}

public:
/// Get the file id for given file. If such file doesn't exist, the result
Expand Down Expand Up @@ -165,7 +191,7 @@ class CompilationUnit {
bool is_module_interface_unit();

/// Return all diagnostics in the process of compilation.
auto diagnostics() -> llvm::ArrayRef<Diagnostic>;
auto diagnostics() -> std::vector<Diagnostic>&;

auto top_level_decls() -> llvm::ArrayRef<clang::Decl*>;

Expand Down Expand Up @@ -196,10 +222,22 @@ class CompilationUnit {
/// Get symbol ID for given marco.
index::SymbolID getSymbolID(const clang::MacroInfo* macro);

private:
Kind kind;
protected:
Self* self;
};

Impl* impl;
/// All AST related information needed for language server.
class CompilationUnit : public CompilationUnitRef {
public:
explicit CompilationUnit(Self* impl) : CompilationUnitRef(impl) {}

CompilationUnit(const CompilationUnit&) = delete;

CompilationUnit(CompilationUnit&& other) : CompilationUnitRef(other.self) {
other.self = nullptr;
}

~CompilationUnit();
};

} // namespace clice
17 changes: 0 additions & 17 deletions include/Compiler/Diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@
#include <string>

#include "AST/SourceCode.h"
#include "Compiler/Tidy.h"

#include "clang/Basic/Diagnostic.h"

namespace clang {

class DiagnosticConsumer;

}

namespace clice {

Expand Down Expand Up @@ -58,11 +49,6 @@ struct DiagnosticID {
bool is_unused() const;
};

class DiagnosticCollector : public clang::DiagnosticConsumer {
public:
tidy::ClangTidyChecker* checker = nullptr;
};

struct Diagnostic {
/// The diagnostic id.
DiagnosticID id;
Expand All @@ -76,9 +62,6 @@ struct Diagnostic {

/// The error message of this diagnostic.
std::string message;

static std::unique_ptr<DiagnosticCollector>
create(std::shared_ptr<std::vector<Diagnostic>> diagnostics);
};

} // namespace clice
4 changes: 0 additions & 4 deletions include/Compiler/Directive.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ struct Directive {
std::vector<MacroRef> macros;
std::vector<Pragma> pragmas;
std::vector<Import> imports;

/// Tell preprocessor to collect directives information and store them in `directives`.
static void attach(clang::Preprocessor& pp,
llvm::DenseMap<clang::FileID, Directive>& directives);
};

} // namespace clice
26 changes: 0 additions & 26 deletions include/Compiler/Tidy.h

This file was deleted.

Loading