Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 10 additions & 27 deletions bin/clice.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ cl::opt<unsigned int> port{
cl::desc("The port to connect to"),
};

cl::opt<std::string> resource_dir{
"resource-dir",
cl::cat(category),
cl::value_desc("path"),
cl::desc(R"(The path of the clang resource directory, default is "../../lib/clang/version")"),
};

cl::opt<logging::ColorMode> log_color{
"log-color",
cl::cat(category),
Expand Down Expand Up @@ -104,26 +97,16 @@ bool check_arguments(int argc, const char** argv) {
init_log();

for(int i = 0; i < argc; ++i) {
logging::info("argv[{}] = {}", i, argv[i]);
LOGGING_INFO("argv[{}] = {}", i, argv[i]);
}

// Initialize resource directory
if(resource_dir.empty()) {
logging::info("No resource directory specified, using default resource directory");
// Try to initialize default resource directory
if(auto result = fs::init_resource_dir(argv[0]); !result) {
logging::warn("Cannot find default resource directory, because {}", result.error());
return false;
}
} else {
// Set and check the specified resource directory
fs::resource_dir = resource_dir.getValue();
if(fs::exists(fs::resource_dir)) {
logging::info("Resource directory found: {}", fs::resource_dir);
} else {
logging::warn("Resource directory not found: {}", fs::resource_dir);
return false;
}

LOGGING_INFO("No resource directory specified, using default resource directory");
// Try to initialize default resource directory
if(auto result = fs::init_resource_dir(argv[0]); !result) {
LOGGING_WARN("Cannot find default resource directory, because {}", result.error());
return false;
}
Comment thread
16bit-ykiko marked this conversation as resolved.
Resolved

return true;
Expand Down Expand Up @@ -151,13 +134,13 @@ int main(int argc, const char** argv) {
switch(mode) {
case Mode::Pipe: {
async::net::listen(loop);
logging::info("Server starts listening on stdin/stdout");
LOGGING_INFO("Server starts listening on stdin/stdout");
break;
}

case Mode::Socket: {
async::net::listen(host.c_str(), port, loop);
logging::info("Server starts listening on {}:{}", host.getValue(), port.getValue());
LOGGING_INFO("Server starts listening on {}:{}", host.getValue(), port.getValue());
break;
}

Expand All @@ -169,7 +152,7 @@ int main(int argc, const char** argv) {

async::run();

logging::info("clice exit normally!");
LOGGING_INFO("clice exit normally!");

return 0;
}
18 changes: 4 additions & 14 deletions bin/unit_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace {

namespace cl = llvm::cl;

cl::OptionCategory unittest_category("Clice Unittest Options");
cl::OptionCategory unittest_category("clice Unittest Options");

cl::opt<std::string> test_dir{
"test-dir",
Expand All @@ -28,12 +28,6 @@ cl::opt<std::string> test_dir{
cl::cat(unittest_category),
};

cl::opt<std::string> resource_dir{
"resource-dir",
cl::desc("Resource dir path"),
cl::cat(unittest_category),
};

cl::opt<std::string> test_filter{
"test-filter",
cl::desc("A glob pattern to run subset of tests"),
Expand Down Expand Up @@ -198,13 +192,9 @@ int main(int argc, const char* argv[]) {
}
}

if(!resource_dir.empty()) {
fs::resource_dir = resource_dir;
} else {
if(auto result = fs::init_resource_dir(argv[0]); !result) {
std::println("Failed to get resource directory, because {}", result.error());
return 1;
}
if(auto result = fs::init_resource_dir(argv[0]); !result) {
std::println("Failed to get resource directory, because {}", result.error());
return 1;
}

using namespace clice::testing;
Expand Down
19 changes: 18 additions & 1 deletion include/Support/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,27 @@ void warn(logging_format<Args...> fmt, Args&&... args) {
}

template <typename... Args>
void fatal [[noreturn]] (logging_format<Args...> fmt, Args&&... args) {
void err(logging_format<Args...> fmt, Args&&... args) {
logging::log(spdlog::level::err, fmt.location, fmt.str, std::forward<Args>(args)...);
}

template <typename... Args>
void critical [[noreturn]] (logging_format<Args...> fmt, Args&&... args) {
logging::log(spdlog::level::critical, fmt.location, fmt.str, std::forward<Args>(args)...);
spdlog::shutdown();
std::exit(1);
}

} // namespace clice::logging

#define LOGGING_MESSAGE(name, fmt, ...) \
if(clice::logging::options.level <= clice::logging::Level::name) { \
clice::logging::name(fmt __VA_OPT__(, ) __VA_ARGS__); \
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

#define LOGGING_TRACE(fmt, ...) LOGGING_MESSAGE(trace, fmt, __VA_ARGS__)
#define LOGGING_DEBUG(fmt, ...) LOGGING_MESSAGE(debug, fmt, __VA_ARGS__)
#define LOGGING_INFO(fmt, ...) LOGGING_MESSAGE(info, fmt, __VA_ARGS__)
#define LOGGING_WARN(fmt, ...) LOGGING_MESSAGE(warn, fmt, __VA_ARGS__)
#define LOGGING_ERROR(fmt, ...) LOGGING_MESSAGE(err, fmt, __VA_ARGS__)
#define LOGGING_FATAL(fmt, ...) clice::logging::critical(fmt __VA_OPT__(, ) __VA_ARGS__);
6 changes: 0 additions & 6 deletions src/AST/Selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@

namespace clice {

#ifdef NDEBUG
#define LOGGING_DEBUG(...)
#else
#define LOGGING_DEBUG(...) logging::debug(__VA_ARGS__)
#endif

namespace {

using Node = SelectionTree::Node;
Expand Down
2 changes: 1 addition & 1 deletion src/Async/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ handle::~handle() {
uv_fs_t request;
int error = uv_fs_close(async::loop, &request, file, nullptr);
if(error < 0) {
logging::warn("Failed to close file: {}", uv_strerror(error));
LOGGING_WARN("Failed to close file: {}", uv_strerror(error));
}
uv_fs_req_cleanup(&request);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Async/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {

/// If an error occurred while reading, we can't continue.
if(nread < 0) [[unlikely]] {
logging::fatal("An error occurred while reading: {0}", uv_strerror(nread));
LOGGING_FATAL("An error occurred while reading: {0}", uv_strerror(nread));
}

/// We have at most one connection and use default event loop. So there is no data race
Expand All @@ -57,7 +57,7 @@ void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
task.dispose();
} else {
/// If the message is invalid, we can't continue.
logging::fatal("Unexpected JSON input: {0}", result);
LOGGING_FATAL("Unexpected JSON input: {0}", result);
}

/// Remove the processed message from the buffer.
Expand Down Expand Up @@ -130,7 +130,7 @@ struct write {

uv_write(&req, writer, buf, 2, [](uv_write_t* req, int status) {
if(status < 0) {
logging::fatal("An error occurred while writing: {0}", uv_strerror(status));
LOGGING_FATAL("An error occurred while writing: {0}", uv_strerror(status));
}

auto& awaiter = uv_cast<struct write>(req);
Expand Down
10 changes: 5 additions & 5 deletions src/Async/libuv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ const std::error_category& category() {
/// Use source_location to log the file, line, and function name where the error occurred.
void uv_check_result(const int result, const std::source_location location) {
if(result < 0) {
logging::warn("libuv error: {}", uv_strerror(result));
logging::warn("At {}:{}:{}",
location.file_name(),
location.line(),
location.function_name());
LOGGING_WARN("libuv error: {}", uv_strerror(result));
LOGGING_WARN("At {}:{}:{}",
location.file_name(),
location.line(),
location.function_name());
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/Compiler/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ struct CompilationDatabase::Impl {

using Arg = std::unique_ptr<llvm::opt::Arg>;
auto on_error = [&](int index, int count) {
logging::warn("missing argument index: {}, count: {} when parse: {}",
index,
count,
file);
LOGGING_WARN("missing argument index: {}, count: {} when parse: {}",
index,
count,
file);
};

/// Prepare for removing arguments.
Expand Down Expand Up @@ -284,7 +284,7 @@ struct CompilationDatabase::Impl {
// /path/to/foobar
if(other.starts_with(dir) &&
(other.size() == dir.size() || path::is_separator(other[dir.size()]))) {
logging::info("Guess command for:{}, from existed file: {}", file, other_file);
LOGGING_INFO("Guess command for:{}, from existed file: {}", file, other_file);
return LookupInfo{info.directory, info.arguments};
}
}
Expand Down Expand Up @@ -492,7 +492,7 @@ auto CompilationDatabase::update_command(llvm::StringRef directory,
/// Handle response file.
if(argument.starts_with("@")) {
if(!response_file.empty()) {
logging::warn(
LOGGING_WARN(
"clice currently supports only one response file in the command, when loads {}",
file);
}
Expand Down Expand Up @@ -624,25 +624,25 @@ auto CompilationDatabase::load_compile_database(llvm::ArrayRef<std::string> comp
std::string filepath = path::join(dir, "compile_commands.json");
auto content = fs::read(filepath);
if(!content) {
logging::warn("Failed to read CDB file: {}, {}", filepath, content.error());
LOGGING_WARN("Failed to read CDB file: {}, {}", filepath, content.error());
return false;
}

auto load = this->load_commands(*content, workspace);
if(!load) {
logging::warn("Failed to load CDB file: {}. {}", filepath, load.error());
LOGGING_WARN("Failed to load CDB file: {}. {}", filepath, load.error());
return false;
}

logging::info("Load CDB file: {} successfully, {} items loaded", filepath, load->size());
LOGGING_INFO("Load CDB file: {} successfully, {} items loaded", filepath, load->size());
return true;
};

if(std::ranges::any_of(compile_commands_dirs, try_load)) {
return;
}

logging::warn(
LOGGING_WARN(
"Can not found any valid CDB file from given directories, search recursively from workspace: {} ...",
workspace);

Expand Down Expand Up @@ -673,7 +673,7 @@ auto CompilationDatabase::load_compile_database(llvm::ArrayRef<std::string> comp
}

/// TODO: Add a default command in clice.toml. Or load commands from .clangd ?
logging::warn(
LOGGING_WARN(
"Can not found any valid CDB file in current workspace, fallback to default mode.");
}

Expand Down Expand Up @@ -711,7 +711,7 @@ auto CompilationDatabase::lookup(llvm::StringRef file, CommandOptions options) -
record(system_header);
}
} else if(!options.suppress_logging) {
logging::warn("Failed to query driver:{}, error:{}", driver, driver_info.error());
LOGGING_WARN("Failed to query driver:{}, error:{}", driver, driver_info.error());
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Compiler/Tidy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,11 @@ std::unique_ptr<ClangTidyChecker> configure(clang::CompilerInstance& instance,
return nullptr;
}
auto file_name = input.getFile();
logging::info("Tidy configure file: {}", file_name);
LOGGING_INFO("Tidy configure file: {}", file_name);

tidy::ClangTidyOptions opts = create_options();
if(opts.Checks) {
logging::info("Tidy configure checks: {}", *opts.Checks);
LOGGING_INFO("Tidy configure checks: {}", *opts.Checks);
}

{
Expand Down Expand Up @@ -391,7 +391,7 @@ std::unique_ptr<ClangTidyChecker> configure(clang::CompilerInstance& instance,
checker->context.setCurrentFile(file_name);
checker->context.setSelfContainedDiags(true);
checker->checks = factories.createChecksForLanguage(&checker->context);
logging::info("Tidy configure checks: {}", checker->checks.size());
LOGGING_INFO("Tidy configure checks: {}", checker->checks.size());
clang::Preprocessor* pp = &instance.getPreprocessor();
for(const auto& check: checker->checks) {
check->registerPPCallbacks(instance.getSourceManager(), pp, pp);
Expand Down
4 changes: 2 additions & 2 deletions src/Compiler/Toolchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ auto query_driver(llvm::StringRef driver) -> std::expected<QueryResult, QueryDri
bool keep_output_file = true;
auto clean_up = llvm::make_scope_exit([&output_path, &keep_output_file]() {
if(keep_output_file) {
logging::warn("Query driver failed, output file:{}", output_path);
LOGGING_WARN("Query driver failed, output file:{}", output_path);
return;
}

if(auto errc = llvm::sys::fs::remove(output_path)) {
logging::warn("Fail to remove temporary file: {}", errc.message());
LOGGING_WARN("Fail to remove temporary file: {}", errc.message());
}
});

Expand Down
6 changes: 3 additions & 3 deletions src/Feature/Diagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ json::Value diagnostics(PositionEncodingKind kind, PathMapping mapping, Compilat
if(level == DiagnosticLevel::Note || level == DiagnosticLevel::Remark) {
/// FIXME: figure out why it may be invalid.
if(fid.isInvalid()) {
logging::info("code: {}, message: {}",
raw_diagnostic.id.diagnostic_code(),
raw_diagnostic.message);
LOGGING_INFO("code: {}, message: {}",
raw_diagnostic.id.diagnostic_code(),
raw_diagnostic.message);
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Feature/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ std::vector<proto::TextEdit> document_format(llvm::StringRef file,
range ? tooling::Range(range->begin, range->length()) : tooling::Range(0, content.size());
auto replacements = format(file, content, selection);
if(!replacements) {
logging::info("Fail to format for {}\n{}", file, replacements.error());
LOGGING_INFO("Fail to format for {}\n{}", file, replacements.error());
return edits;
}

Expand Down
Loading