Skip to content

Commit 38edfdf

Browse files
committed
Generator: improve the -help, and accept a compile_command.json in -b
Note: the awkward use of unique_ptr is there to support old version of libclang
1 parent 7635813 commit 38edfdf

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

generator/main.cpp

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include "llvm/Support/CommandLine.h"
2424
#include "clang/Frontend/FrontendActions.h"
25-
#include "clang/Tooling/CompilationDatabase.h"
25+
#include "clang/Tooling/JSONCompilationDatabase.h"
2626
#include "clang/Tooling/Tooling.h"
2727
#include "clang/AST/ASTContext.h"
2828

@@ -49,38 +49,56 @@ namespace cl = llvm::cl;
4949

5050
cl::opt<std::string> BuildPath(
5151
"b",
52-
cl::desc("<build-path>"),
52+
cl::value_desc("compile_commands.json"),
53+
cl::desc("Path to the compilation database (compile_commands.json) If this argument is not passed, the compilation arguments can be passed on the command line after '--'"),
5354
cl::Optional);
5455

5556
cl::list<std::string> SourcePaths(
5657
cl::Positional,
57-
cl::desc("(<source0> [... <sourceN>])|<path>"),
58+
cl::desc("<sources>* [-- <compile command>]"),
5859
cl::ZeroOrMore);
5960

6061
cl::opt<std::string> OutputPath(
6162
"o",
62-
cl::desc("<output path>"),
63+
cl::value_desc("output path"),
64+
cl::desc("Output directory where the generated files will be put"),
6365
cl::Required);
6466

6567
cl::list<std::string> ProjectPaths(
6668
"p",
67-
cl::desc("<project>:<path>[:<revision>]"),
69+
cl::value_desc("<project>:<path>[:<revision>]"),
70+
cl::desc("Project specification: The name of the project, the absolute path of the source code, and the revision separated by colons. Example: -p projectname:/path/to/source/code:0.3beta"),
6871
cl::ZeroOrMore);
6972

7073

7174
cl::list<std::string> ExternalProjectPaths(
7275
"e",
73-
cl::desc("<project>:<path>:<url>"),
76+
cl::value_desc("<project>:<path>:<url>"),
77+
cl::desc("Reference to an external project. Example: -e clang/include/clang:/opt/llvm/include/clang/:https://code.woboq.org/llvm"),
7478
cl::ZeroOrMore);
7579

7680
cl::opt<std::string> DataPath(
7781
"d",
78-
cl::desc("<data path>"),
82+
cl::value_desc("data path"),
83+
cl::desc("Data url where all the javascript and css files are found. Can be absolute, or relative to the output directory. Defaults to ../data"),
7984
cl::Optional);
8085

8186
cl::opt<bool> ProcessAllSources(
8287
"a",
83-
cl::desc("Process all sources in the compilation_database.json (should not have sources then)"));
88+
cl::desc("Process all files from the compile_commands.json. If this argument is passed, the list of sources does not need to be passed"));
89+
90+
cl::extrahelp extra(
91+
92+
R"(
93+
94+
EXAMPLES:
95+
96+
Simple generation without compile command or project (compile command specified inline)
97+
codebrowser_generator -o ~/public_html/code -d https://code.woboq.org/data $PWD -- -std=c++14 -I/opt/llvm/include
98+
99+
With a project
100+
codebrowser_generator -b $PWD/compile_commands.js -a -p codebrowser:$PWD -o ~/public_html/code
101+
)");
84102

85103
#if 1
86104
std::string locationToString(clang::SourceLocation loc, clang::SourceManager& sm) {
@@ -306,10 +324,19 @@ int main(int argc, const char **argv) {
306324
BrowserAction::projectManager = &projectManager;
307325

308326

309-
if (!Compilations) {
327+
if (!Compilations && llvm::sys::fs::exists(BuildPath)) {
310328
std::string ErrorMessage;
311-
Compilations = std::unique_ptr<clang::tooling::CompilationDatabase>(
312-
clang::tooling::CompilationDatabase::loadFromDirectory(BuildPath, ErrorMessage));
329+
if (llvm::sys::fs::is_directory(BuildPath)) {
330+
Compilations = std::unique_ptr<clang::tooling::CompilationDatabase>(
331+
clang::tooling::CompilationDatabase::loadFromDirectory(BuildPath, ErrorMessage));
332+
} else {
333+
Compilations = std::unique_ptr<clang::tooling::CompilationDatabase>(
334+
clang::tooling::JSONCompilationDatabase::loadFromFile(BuildPath, ErrorMessage
335+
#if CLANG_VERSION_MAJOR >= 4
336+
, clang::tooling::JSONCommandLineSyntax::AutoDetect
337+
#endif
338+
));
339+
}
313340
if (!ErrorMessage.empty()) {
314341
std::cerr << ErrorMessage << std::endl;
315342
}

0 commit comments

Comments
 (0)