Skip to content

Commit bbbe9db

Browse files
committed
more file filtering
1 parent 76cf073 commit bbbe9db

File tree

5 files changed

+117
-63
lines changed

5 files changed

+117
-63
lines changed

README.adoc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,17 @@ cmake --build . -j <threads>
2828
cmake --install .
2929
----
3030

31-
31+
=== Doxygen:
32+
* tries to work for many languages
33+
* use the inferior libclang API
34+
* old program with lots of technical debt
35+
* not written by me
36+
37+
=== MrDox:
38+
* narrow and deep focus on C++ only
39+
* uses clang's unstable libtooling API:
40+
** Understands ALL C++: if clang can compile it, MrDox knows about it
41+
** This includes up to C++20 and even experimental things in C++23
42+
* brand new program with no technical debt
43+
* written by me
3244

include/mrdox/Config.hpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#ifndef MRDOX_CONFIG_HPP
1313
#define MRDOX_CONFIG_HPP
1414

15+
#include <mrdox/Errors.hpp>
1516
#include <clang/Tooling/ArgumentsAdjusters.h>
1617
#include <clang/Tooling/Execution.h>
1718
#include <llvm/ADT/Optional.h>
@@ -31,6 +32,10 @@ namespace mrdox {
3132
*/
3233
struct Config
3334
{
35+
/** The root path from which all relative paths are calculated.
36+
*/
37+
llvm::SmallString<16> configPath;
38+
3439
/** Adjustments to tool command line, applied during execute.
3540
*/
3641
tooling::ArgumentsAdjuster ArgAdjuster;
@@ -48,7 +53,7 @@ struct Config
4853
// Directory where processed files are stored. Links
4954
// to definition locations will only be generated if
5055
// the file is in this dir.
51-
std::vector<llvm::SmallString<0>> includePaths;
56+
std::vector<std::string> includePaths;
5257

5358
// URL of repository that hosts code used
5459
// for links to definition locations.
@@ -57,7 +62,7 @@ struct Config
5762
bool IgnoreMappingFailures = false;
5863

5964
public:
60-
Config();
65+
Config() = default;
6166
Config(Config&&) = delete;
6267
Config& operator=(Config&&) = delete;
6368

@@ -68,7 +73,7 @@ struct Config
6873
should be be removed for matching files.
6974
*/
7075
bool
71-
filterFile(
76+
filterSourceFile(
7277
llvm::StringRef filePath,
7378
llvm::SmallVectorImpl<char>& prefixPath) const noexcept;
7479

@@ -77,11 +82,10 @@ struct Config
7782

7883
filter namespaces, files, entities;
7984

80-
std::error_code
81-
load(
82-
const std::string & name,
83-
const std::source_location & loc =
84-
std::source_location::current());
85+
bool
86+
loadFromFile(
87+
llvm::StringRef filePath,
88+
Reporter& R);
8589
};
8690

8791
} // mrdox

source/lib/Config.cpp

Lines changed: 72 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
//
1111

1212
#include <mrdox/Config.hpp>
13+
#include <llvm/Support/FileSystem.h>
14+
#include <llvm/Support/MemoryBuffer.h>
15+
#include <llvm/Support/Path.h>
1316
#include <llvm/Support/YAMLParser.h>
1417
#include <llvm/Support/YAMLTraits.h>
1518

@@ -18,10 +21,9 @@ struct llvm::yaml::MappingTraits<
1821
clang::mrdox::Config::filter>
1922
{
2023
static void mapping(
21-
IO &io, clang::mrdox::Config::filter &f)
24+
IO &io, clang::mrdox::Config::filter& f)
2225
{
23-
io.mapOptional("include", f.include);
24-
io.mapOptional("exclude", f.exclude);
26+
io.mapOptional("exclude", f);
2527
}
2628
};
2729

@@ -38,6 +40,7 @@ struct llvm::yaml::MappingTraits<
3840
io.mapOptional("project-name", f.ProjectName);
3941
io.mapOptional("public-only", f.PublicOnly);
4042
io.mapOptional("output-dir", f.OutDirectory);
43+
io.mapOptional("include", f.includePaths);
4144
}
4245
};
4346

@@ -46,58 +49,92 @@ struct llvm::yaml::MappingTraits<
4649
namespace clang {
4750
namespace mrdox {
4851

49-
Config::
50-
Config()
51-
{
52-
if (auto p = ::getenv("MRDOX_REPOSITORY_URL"))
53-
RepositoryUrl.emplace(p);
54-
else if (auto q = ::getenv("DRONE_REMOTE_URL"))
55-
RepositoryUrl.emplace(q);
56-
}
57-
5852
bool
5953
Config::
60-
filterFile(
54+
filterSourceFile(
6155
llvm::StringRef filePath,
6256
llvm::SmallVectorImpl<char>& prefixPath) const noexcept
6357
{
6458
namespace path = llvm::sys::path;
6559

66-
for(auto const s : includePaths)
60+
llvm::SmallString<32> temp;
61+
for(auto const& s : includePaths)
6762
{
68-
if(filePath.starts_with(s))
63+
temp = filePath;
64+
if(path::replace_path_prefix(temp, s, ""))
6965
{
70-
prefixPath = s;
71-
if(! path::is_separator(prefixPath.back()))
72-
{
73-
auto const sep = path::get_separator();
74-
prefixPath.insert(prefixPath.end(),
75-
sep.begin(), sep.end());
76-
}
66+
prefixPath.assign(s.begin(), s.end());
7767
return false;
7868
}
7969
}
8070
return true;
8171
}
8272

83-
std::error_code
73+
// Make the path end in a separator
74+
static
75+
void
76+
makeDirsy(
77+
llvm::SmallVectorImpl<char>& s)
78+
{
79+
namespace path = llvm::sys::path;
80+
if(! path::is_separator(s.back()))
81+
{
82+
auto const sep = path::get_separator();
83+
s.insert(s.end(), sep.begin(), sep.end());
84+
}
85+
}
86+
87+
bool
8488
Config::
85-
load(
86-
const std::string & name,
87-
const std::source_location & loc)
89+
loadFromFile(
90+
llvm::StringRef filePath,
91+
Reporter& R)
8892
{
89-
#if 0
90-
auto ct = llvm::MemoryBuffer::getFile(SourceRoot + "/" + name);
91-
if (!ct)
92-
return ct.getError();
93+
namespace fs = llvm::sys::fs;
94+
namespace path = llvm::sys::path;
9395

94-
llvm::yaml::Input yin{**ct};
96+
// get absolute path to config file
97+
configPath = filePath;
98+
fs::make_absolute(configPath);
9599

100+
// Read the YAML file and apply to this
101+
auto fileText = llvm::MemoryBuffer::getFile(configPath);
102+
if(! fileText)
103+
{
104+
R.failed("llvm::MemoryBuffer::getFile", fileText);
105+
return false;
106+
}
107+
llvm::yaml::Input yin(**fileText);
96108
yin >> *this;
97-
if ( yin.error() )
98-
return yin.error();
99-
#endif
100-
return {};
109+
std::error_code ec = yin.error();
110+
if(ec)
111+
{
112+
R.failed("llvm::yaml::Input::operator>>", ec);
113+
return false;
114+
}
115+
116+
// change configPath to the directory
117+
path::remove_filename(configPath);
118+
path::remove_dots(configPath, true);
119+
120+
// make includePaths absolute
121+
{
122+
llvm::SmallString<128> temp;
123+
for(auto& s : includePaths)
124+
{
125+
if(path::is_absolute(s))
126+
continue;
127+
temp.clear();
128+
path::append(temp, configPath, s);
129+
// separator at the end is required
130+
// for replace_path_prefix to work
131+
path::remove_dots(temp, true);
132+
makeDirsy(temp);
133+
s.assign(temp.begin(), temp.end());
134+
}
135+
}
136+
137+
return true;
101138
}
102139

103140
} // mrdox

source/lib/Visitor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ mapDecl(T const* D)
7070
// new element
7171
filePath = loc.getFilename();
7272
FileFilter& ff = result.first->second;
73-
ff.exclude = config_.filterFile(filePath, ff.prefix);
73+
ff.exclude = config_.filterSourceFile(filePath, ff.prefix);
7474
if(ff.exclude)
7575
return true;
7676
path::replace_path_prefix(filePath, ff.prefix, "");

source/tool/ToolMain.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ R"(Generates documentation from source code and comments.
4141
Examples
4242
4343
$ mrdox mrdox.yml
44-
$ mrdox --output ./docs mrdox.yml
44+
$ mrdox --config=mrdox.yml --output ./docs
4545
)";
4646

4747
static
@@ -53,6 +53,22 @@ static
5353
llvm::cl::OptionCategory
5454
ToolCategory("mrdox options");
5555

56+
static
57+
llvm::cl::opt<std::string>
58+
ConfigPath(
59+
"config",
60+
llvm::cl::desc(R"(The config filename relative to the repository root)"),
61+
llvm::cl::init("mrdox.yaml"),
62+
llvm::cl::cat(ToolCategory));
63+
64+
static
65+
llvm::cl::opt<std::string>
66+
FormatType(
67+
"format",
68+
llvm::cl::desc("Format for outputted docs (\"adoc\" or \"xml\")."),
69+
llvm::cl::init("adoc"),
70+
llvm::cl::cat(ToolCategory));
71+
5672
static
5773
llvm::cl::opt<bool>
5874
IgnoreMappingFailures(
@@ -69,22 +85,6 @@ OutDirectory(
6985
llvm::cl::init("."),
7086
llvm::cl::cat(ToolCategory));
7187

72-
static
73-
llvm::cl::opt<std::string>
74-
ConfigPath(
75-
"config-file",
76-
llvm::cl::desc(R"(The config filename relative to the repository root)"),
77-
llvm::cl::init("mrdox.yaml"),
78-
llvm::cl::cat(ToolCategory));
79-
80-
static
81-
llvm::cl::opt<std::string>
82-
FormatType(
83-
"format",
84-
llvm::cl::desc("Format for outputted docs (\"adoc\" or \"xml\")."),
85-
llvm::cl::init("adoc"),
86-
llvm::cl::cat(ToolCategory));
87-
8888
} // (anon)
8989

9090
//------------------------------------------------
@@ -118,7 +118,8 @@ toolMain(int argc, const char** argv)
118118
config.OutDirectory = OutDirectory;
119119
config.IgnoreMappingFailures = IgnoreMappingFailures;
120120

121-
config.load(ConfigPath);
121+
if(! config.loadFromFile(ConfigPath, R))
122+
return EXIT_FAILURE;
122123

123124
// create the executor
124125
auto ex = std::make_unique<tooling::AllTUsToolExecutor>(

0 commit comments

Comments
 (0)