Skip to content

Commit 2fb56a2

Browse files
vinniefalcoklemens-morgenstern
authored andcommitted
combine tool, tests, and library
1 parent d9efa40 commit 2fb56a2

File tree

6 files changed

+858
-137
lines changed

6 files changed

+858
-137
lines changed

source/tool/GenerateAction.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//
2+
// This is a derivative work. originally part of the LLVM Project.
3+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
// Copyright (c) 2023 Vinnie Falco ([email protected])
8+
//
9+
// Official repository: https://github.com/cppalliance/mrdox
10+
//
11+
12+
#include "Options.hpp"
13+
#include "api/ConfigImpl.hpp"
14+
#include <mrdox/Generators.hpp>
15+
#include <mrdox/Reporter.hpp>
16+
#include <clang/Tooling/AllTUsExecution.h>
17+
#include <clang/Tooling/JSONCompilationDatabase.h>
18+
#include <cstdlib>
19+
20+
namespace clang {
21+
namespace mrdox {
22+
23+
int
24+
DoGenerateAction(Reporter& R)
25+
{
26+
auto& generators = getGenerators();
27+
28+
// Calculate additional YAML settings from command line options.
29+
std::string extraYaml;
30+
{
31+
llvm::raw_string_ostream os(extraYaml);
32+
if(IgnoreMappingFailures.getValue())
33+
os << "ignore-failures: true\n";
34+
}
35+
36+
// Load configuration file
37+
if(! ConfigPath.hasArgStr())
38+
{
39+
llvm::errs() <<
40+
"Missing configuration file path argument.\n";
41+
return EXIT_FAILURE;
42+
}
43+
std::error_code ec;
44+
auto config = loadConfigFile(ConfigPath, extraYaml, ec);
45+
if(ec)
46+
{
47+
(void)R.error(ec, "load config file '", ConfigPath, "'");
48+
return EXIT_FAILURE;
49+
}
50+
51+
// Load the compilation database
52+
if(InputPaths.empty())
53+
{
54+
llvm::errs() <<
55+
"Missing path to compilation database argument.\n";
56+
return EXIT_FAILURE;
57+
}
58+
if(InputPaths.size() > 1)
59+
{
60+
llvm::errs() <<
61+
"Expected one input path argument, got more than one.\n";
62+
return EXIT_FAILURE;
63+
}
64+
std::string errorMessage;
65+
auto compilations =
66+
tooling::JSONCompilationDatabase::loadFromFile(
67+
InputPaths.front(), errorMessage,
68+
tooling::JSONCommandLineSyntax::AutoDetect);
69+
if(! compilations)
70+
{
71+
llvm::errs() << errorMessage << '\n';
72+
return EXIT_FAILURE;
73+
}
74+
75+
// Create the ToolExecutor from the compilation database
76+
int ThreadCount = 0;
77+
auto ex = std::make_unique<tooling::AllTUsToolExecutor>(
78+
*compilations, ThreadCount);
79+
80+
// Create the generator
81+
auto generator = generators.find(FormatType.getValue());
82+
if(! generator)
83+
{
84+
R.print("Generator '", FormatType.getValue(), "' not found.");
85+
return EXIT_FAILURE;
86+
}
87+
88+
// Run the tool, this can take a while
89+
auto corpus = Corpus::build(*ex, config, R);
90+
if(R.error(corpus, "build the documentation corpus"))
91+
return EXIT_FAILURE;
92+
93+
// Run the generator.
94+
if(config->verboseOutput)
95+
llvm::outs() << "Generating docs...\n";
96+
auto err = generator->build(OutputPath.getValue(), **corpus, R);
97+
if(err)
98+
{
99+
R.print(err.message(), "generate '", OutputPath, "'");
100+
return EXIT_FAILURE;
101+
}
102+
return EXIT_SUCCESS;
103+
}
104+
105+
} // mrdox
106+
} // clang

source/tool/Options.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//
2+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
3+
// See https://llvm.org/LICENSE.txt for license information.
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
//
6+
// Copyright (c) 2023 Vinnie Falco ([email protected])
7+
//
8+
// Official repository: https://github.com/cppalliance/mrdox
9+
//
10+
11+
#include "Options.hpp"
12+
13+
namespace clang {
14+
namespace mrdox {
15+
16+
char const* Overview =
17+
R"(Generate reference documentation, run tests against
18+
a set of input vectors, or update a set of reference tests.)";
19+
20+
llvm::cl::OptionCategory Category("mrdox options");
21+
22+
llvm::cl::extrahelp ExtraHelp(
23+
R"(Usage:
24+
25+
mrdox .. ( compile-commands )
26+
27+
mrdox .. --action ( "test" | "update" ) ( dir | file )...
28+
29+
Examples
30+
31+
mrdox --action test friend.cpp
32+
33+
mrdox --format adoc compile_commands.json
34+
)");
35+
36+
llvm::cl::opt<Action> ToolAction(
37+
"action",
38+
llvm::cl::desc(R"(Which action should be performed)"),
39+
llvm::cl::init(Action::generate),
40+
llvm::cl::values(
41+
clEnumVal(test, "Compare output against expected"),
42+
clEnumVal(update, "Update all expected xml files"),
43+
clEnumVal(generate, "Generate reference documentation")),
44+
llvm::cl::cat(Category));
45+
46+
// Test options
47+
48+
llvm::cl::opt<bool> badOption(
49+
"bad",
50+
llvm::cl::desc("Write a .bad.xml file for each test failure"),
51+
llvm::cl::init(true),
52+
llvm::cl::cat(Category));
53+
54+
llvm::cl::opt<bool> adocOption(
55+
"adoc",
56+
llvm::cl::desc("Write the corresponding Asciidoc (adoc) file for each input test file"),
57+
llvm::cl::init(false),
58+
llvm::cl::cat(Category));
59+
60+
// Generate options
61+
62+
llvm::cl::opt<std::string> FormatType(
63+
"format",
64+
llvm::cl::desc("Format for outputted docs (\"adoc\" or \"xml\")."),
65+
llvm::cl::init("adoc"),
66+
llvm::cl::cat(Category));
67+
68+
// Common options
69+
70+
llvm::cl::opt<bool> IgnoreMappingFailures(
71+
"ignore-map-errors",
72+
llvm::cl::desc("Continue if files are not mapped correctly."),
73+
llvm::cl::init(true),
74+
llvm::cl::cat(Category));
75+
76+
llvm::cl::opt<std::string> ConfigPath(
77+
"config",
78+
llvm::cl::desc(R"(The config filename relative to the repository root)"),
79+
llvm::cl::init("mrdox.yaml"),
80+
llvm::cl::cat(Category));
81+
82+
llvm::cl::opt<std::string> OutputPath(
83+
"output",
84+
llvm::cl::desc("Directory or file for generating output."),
85+
llvm::cl::init("."),
86+
llvm::cl::cat(Category));
87+
88+
llvm::cl::list<std::string> InputPaths(
89+
"inputs",
90+
llvm::cl::Sink,
91+
llvm::cl::desc("The path to the compilation database, or one or more .cpp files to test."),
92+
llvm::cl::cat(Category));
93+
94+
} // mrdox
95+
} // clang

source/tool/Options.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
3+
// See https://llvm.org/LICENSE.txt for license information.
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
//
6+
// Copyright (c) 2023 Vinnie Falco ([email protected])
7+
//
8+
// Official repository: https://github.com/cppalliance/mrdox
9+
//
10+
11+
#ifndef MRDOX_TOOL_OPTIONS_HPP
12+
#define MRDOX_TOOL_OPTIONS_HPP
13+
14+
#include <llvm/Support/CommandLine.h>
15+
#include <string>
16+
17+
namespace clang {
18+
namespace mrdox {
19+
20+
enum Action : int
21+
{
22+
test,
23+
update,
24+
generate
25+
};
26+
27+
extern char const* Overview;
28+
extern llvm::cl::OptionCategory Category;
29+
extern llvm::cl::extrahelp ExtraHelp;
30+
extern llvm::cl::opt<Action> ToolAction;
31+
32+
// Test options
33+
extern llvm::cl::opt<bool> badOption;
34+
extern llvm::cl::opt<bool> adocOption;
35+
36+
// Generate options
37+
extern llvm::cl::opt<std::string> FormatType;
38+
39+
// Common options
40+
extern llvm::cl::opt<bool> IgnoreMappingFailures;
41+
extern llvm::cl::opt<std::string> ConfigPath;
42+
extern llvm::cl::opt<std::string> OutputPath;
43+
extern llvm::cl::list<std::string> InputPaths;
44+
45+
} // mrdox
46+
} // clang
47+
48+
#endif

source/tool/SingleFileDB.hpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
3+
// See https://llvm.org/LICENSE.txt for license information.
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
//
6+
// Copyright (c) 2023 Vinnie Falco ([email protected])
7+
//
8+
// Official repository: https://github.com/cppalliance/mrdox
9+
//
10+
11+
#ifndef MRDOX_TOOL_SINGLEFILEDB_HPP
12+
#define MRDOX_TOOL_SINGLEFILEDB_HPP
13+
14+
#include <clang/Tooling/CompilationDatabase.h>
15+
#include <string>
16+
#include <utility>
17+
#include <vector>
18+
19+
namespace clang {
20+
namespace mrdox {
21+
22+
/** Compilation database for a single .cpp file.
23+
*/
24+
class SingleFileDB
25+
: public tooling::CompilationDatabase
26+
{
27+
std::vector<tooling::CompileCommand> cc_;
28+
29+
public:
30+
SingleFileDB(
31+
llvm::StringRef dir,
32+
llvm::StringRef file)
33+
{
34+
std::vector<std::string> cmds;
35+
cmds.emplace_back("clang");
36+
cmds.emplace_back("-std=c++20");
37+
cmds.emplace_back("-pedantic-errors");
38+
cmds.emplace_back("-Werror");
39+
cmds.emplace_back(file);
40+
cc_.emplace_back(
41+
dir,
42+
file,
43+
std::move(cmds),
44+
dir);
45+
cc_.back().Heuristic = "unit test";
46+
}
47+
48+
std::vector<tooling::CompileCommand>
49+
getCompileCommands(
50+
llvm::StringRef FilePath) const override
51+
{
52+
if(! FilePath.equals(cc_.front().Filename))
53+
return {};
54+
return { cc_.front() };
55+
}
56+
57+
std::vector<std::string>
58+
getAllFiles() const override
59+
{
60+
return { cc_.front().Filename };
61+
}
62+
63+
std::vector<tooling::CompileCommand>
64+
getAllCompileCommands() const override
65+
{
66+
return { cc_.front() };
67+
}
68+
};
69+
70+
} // mrdox
71+
} // clang
72+
73+
#endif

0 commit comments

Comments
 (0)