Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
51 changes: 51 additions & 0 deletions src/feature/semantic_tokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "clang/AST/Attr.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/Module.h"

namespace clice::feature {

Expand Down Expand Up @@ -168,6 +169,7 @@ class SemanticTokensCollector : public SemanticVisitor<SemanticTokensCollector>
auto collect() -> std::vector<RawToken> {
highlight_lexical(unit.interested_file());
run();
highlight_modules();
merge_tokens();
return std::move(tokens);
}
Expand Down Expand Up @@ -291,6 +293,55 @@ class SemanticTokensCollector : public SemanticVisitor<SemanticTokensCollector>
});
}

void highlight_modules() {
auto interested = unit.interested_file();

// Import statements: highlight module name identifiers.
auto directives_it = unit.directives().find(interested);
if(directives_it != unit.directives().end()) {
for(const auto& import: directives_it->second.imports) {
for(auto loc: import.name_locations) {
add_token(loc, SymbolKind::Module, 0);
}
}
}

// Module declaration (export module foo.bar;): highlight module name.
auto* mod = unit.context().getCurrentNamedModule();
if(!mod) {
return;
}

auto def_loc = mod->DefinitionLoc;
if(!def_loc.isValid() || !def_loc.isFileID()) {
return;
}

auto [fid, offset] = unit.decompose_location(def_loc);
if(fid != interested) {
return;
}

auto content = unit.file_content(fid);
auto& lang_opts = unit.lang_options();
Lexer lexer(content.substr(offset), false, &lang_opts);

// Skip 'module' keyword.
lexer.advance();

// Scan for identifiers (module name parts) until semicolon/eof.
while(true) {
auto token = lexer.advance();
if(token.is_eof() || token.kind == clang::tok::semi) {
break;
}
if(token.is_identifier()) {
auto range = LocalSourceRange(offset + token.range.begin, offset + token.range.end);
tokens.push_back({.range = range, .kind = SymbolKind::Module, .modifiers = 0});
}
Comment thread
16bit-ykiko marked this conversation as resolved.
Resolved
}
}

void highlight_lexical(clang::FileID fid) {
auto content = unit.file_content(fid);
auto& lang_opts = unit.lang_options();
Expand Down
28 changes: 2 additions & 26 deletions src/semantic/semantic_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,32 +132,8 @@ class SemanticVisitor : public FilteredASTVisitor<SemanticVisitor<Derived>> {
}
}

// if(auto module = unit.context().getCurrentNamedModule()) {
// auto keyword = module->DefinitionLoc;
// auto begin = TB.spelledTokenContaining(keyword);
// // assert(begin->kind() == clang::tok::identifier && begin->text(SM) == "module" &&
// // "Invalid module declaration");
//
// begin += 1;
// auto end = TB.spelledTokens(unit.file_id(keyword)).end();
//
// for(auto iter = begin; iter != end; ++iter) {
// if(iter->kind() == clang::tok::identifier) {
// if(auto next = iter + 1; next != end && (next->kind() == clang::tok::period ||
// next->kind() == clang::tok::colon)) {
// iter += 1;
// continue;
// }
//
// end = iter + 1;
// break;
// }
//
// std::unreachable();
// }
//
// handleModuleOccurrence(keyword, llvm::ArrayRef<clang::syntax::Token>(begin, end));
//}
// Module declaration and import handling is done in SemanticTokensCollector
// directly, using directives data and AST rather than TokenBuffer.
}

public:
Expand Down
63 changes: 63 additions & 0 deletions tests/unit/feature/semantic_tokens_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,69 @@ cd*/
ASSERT_EQ(comments[1].length, 4);
}

TEST_CASE(ModuleDeclaration) {
add_main("main.cpp", R"cpp(
export module @mod[foo];
)cpp");
ASSERT_TRUE(compile("-std=c++20"));
tokens = feature::semantic_tokens(*unit, feature::PositionEncoding::UTF8);
decoded = decode_utf8_tokens(unit->interested_content(), tokens);

EXPECT_TOKEN("mod", SymbolKind::Module);
}

TEST_CASE(ModuleDeclarationDotted) {
add_main("main.cpp", R"cpp(
export module @m0[foo].@m1[bar];
)cpp");
ASSERT_TRUE(compile("-std=c++20"));
tokens = feature::semantic_tokens(*unit, feature::PositionEncoding::UTF8);
decoded = decode_utf8_tokens(unit->interested_content(), tokens);

EXPECT_TOKEN("m0", SymbolKind::Module);
EXPECT_TOKEN("m1", SymbolKind::Module);
}

TEST_CASE(ModuleImport) {
// Step 1: Build the module interface to a PCM.
auto pcm_path = fs::createTemporaryFile("test-mod", "pcm");
ASSERT_TRUE(pcm_path.has_value());

{
Tester mod;
mod.add_main("mod.cppm", "export module foo;\nexport int x = 42;\n");
mod.prepare("-std=c++20");
mod.params.kind = CompilationKind::ModuleInterface;
mod.params.output_file = *pcm_path;
auto built = clice::compile(mod.params);
ASSERT_TRUE(built.completed());
}

// Step 2: Compile the importing file with -fmodule-file.
add_main("main.cpp", R"cpp(
import @mod[foo];
int y = x;
)cpp");
prepare("-std=c++20");
auto fmodule_arg = std::string("-fmodule-file=foo=") + *pcm_path;
owned_args.push_back(fmodule_arg);
params.arguments.clear();
for(auto& arg: owned_args) {
params.arguments.push_back(arg.c_str());
}

auto built = clice::compile(params);
ASSERT_TRUE(built.completed());
unit.emplace(std::move(built));

tokens = feature::semantic_tokens(*unit, feature::PositionEncoding::UTF8);
decoded = decode_utf8_tokens(unit->interested_content(), tokens);

EXPECT_TOKEN("mod", SymbolKind::Module);

fs::remove(*pcm_path);
}

}; // TEST_SUITE(SemanticTokens)

} // namespace
Expand Down
Loading