Skip to content

Commit 309b16d

Browse files
committed
refactor config and settings
1 parent c579332 commit 309b16d

File tree

13 files changed

+223
-116
lines changed

13 files changed

+223
-116
lines changed

include/mrdox/Config.hpp

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <llvm/ADT/Optional.h>
1818
#include <llvm/ADT/SmallVector.h>
1919
#include <llvm/ADT/StringRef.h>
20+
#include <llvm/Support/Error.h>
2021
#include <llvm/Support/YAMLTraits.h>
2122
#include <memory>
2223
#include <string>
@@ -35,18 +36,45 @@ namespace mrdox {
3536
3637
This contains all the settings applied from
3738
the command line and the YML file (if any).
39+
A configuration is always connected to a
40+
particular directory from which absolute paths
41+
are calculated from relative paths.
3842
*/
3943
class Config
4044
{
4145
template<class T>
4246
friend struct llvm::yaml::MappingTraits;
4347

48+
struct Options;
49+
50+
llvm::SmallString<0> configDir_;
4451
std::string sourceRoot_;
52+
std::vector<std::string> inputFileFilter_;
53+
54+
explicit Config(llvm::StringRef configDir);
4555

4656
public:
47-
/** The root path from which all relative paths are calculated.
57+
/** Return a defaulted Config using an existing directory.
58+
59+
@param dirPath The path to the directory.
60+
If this is relative, an absolute path will
61+
be calculated from the current directory.
4862
*/
49-
llvm::SmallString<16> configPath;
63+
static
64+
llvm::Expected<std::unique_ptr<Config>>
65+
createAtDirectory(
66+
llvm::StringRef dirPath);
67+
68+
/** Return a Config loaded from the specified YAML file.
69+
*/
70+
static
71+
llvm::Expected<std::unique_ptr<Config>>
72+
loadFromFile(
73+
llvm::StringRef filePath);
74+
75+
//
76+
// VFALCO these naked data members are temporary...
77+
//
5078

5179
/** Adjustments to tool command line, applied during execute.
5280
*/
@@ -69,7 +97,6 @@ class Config
6997
bool IgnoreMappingFailures = false;
7098

7199
public:
72-
Config() = default;
73100
Config(Config&&) = delete;
74101
Config& operator=(Config&&) = delete;
75102

@@ -79,22 +106,44 @@ class Config
79106
//
80107
//--------------------------------------------
81108

109+
/** Return the full path to the configuration directory.
110+
111+
The returned path will always be POSIX
112+
style and have a trailing separator.
113+
*/
114+
llvm::StringRef
115+
configDir() const noexcept
116+
{
117+
return configDir_;
118+
}
119+
120+
/** Return the full path to the source root directory.
121+
122+
The returned path will always be POSIX
123+
style and have a trailing separator.
124+
*/
82125
llvm::StringRef
83126
sourceRoot() const noexcept
84127
{
85128
return sourceRoot_;
86129
}
87130

88-
/** Returns true if the file should be skipped.
131+
/** Returns true if the file should be visited.
89132
90-
If the file is not skipped, then prefixPath
91-
is set to the portion of the file path which
133+
If the file is visited, then prefix is
134+
set to the portion of the file path which
92135
should be be removed for matching files.
136+
137+
@param filePath The posix-style full path
138+
to the file being processed.
139+
140+
@param prefix The prefix which should be
141+
removed from subsequent matches.
93142
*/
94143
bool
95-
filterSourceFile(
144+
shouldVisitFile(
96145
llvm::StringRef filePath,
97-
llvm::SmallVectorImpl<char>& prefixPath) const noexcept;
146+
llvm::SmallVectorImpl<char>& prefix) const noexcept;
98147

99148
//--------------------------------------------
100149
//
@@ -112,15 +161,11 @@ class Config
112161
setSourceRoot(
113162
llvm::StringRef dirPath);
114163

115-
public:
116-
struct filter { std::vector<std::string> include, exclude; };
117-
118-
filter namespaces, files, entities;
119-
120-
bool
121-
loadFromFile(
122-
llvm::StringRef filePath,
123-
Reporter& R);
164+
/** Set the filter for including translation units.
165+
*/
166+
llvm::Error
167+
setInputFileFilter(
168+
std::vector<std::string> const& list);
124169
};
125170

126171
} // mrdox

include/mrdox/Error.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ namespace mrdox {
2323

2424
/** Return an Error with descriptive information.
2525
26-
@param action A phrase describing the attempted action.
26+
@param reason A phrase describing the cause of the failure.
2727
2828
@param loc The source location where the failure occurred.
2929
*/
3030
[[nodiscard]]
3131
llvm::Error
3232
makeErrorString(
33-
std::string action,
33+
std::string reason,
3434
std::source_location loc =
3535
std::source_location::current());
3636

include/mrdox/Reporter.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ struct Reporter
143143

144144
static bool isFailure(llvm::Error&& e) noexcept
145145
{
146-
return ! e.operator bool();
146+
return e.operator bool();
147147
}
148148

149149
static bool isFailure(std::error_code&& ec) noexcept

source/lib/ClangDoc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//
1717

1818
#include "ClangDoc.h"
19-
#include <mrdox/Visitor.hpp>
19+
#include "Visitor.hpp"
2020
#include <clang/Frontend/CompilerInstance.h>
2121
#include <clang/Frontend/FrontendAction.h>
2222

source/lib/Config.cpp

Lines changed: 98 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,28 @@
1818
#include <llvm/Support/YAMLParser.h>
1919
#include <llvm/Support/YAMLTraits.h>
2020

21+
namespace clang {
22+
namespace mrdox {
23+
24+
struct Config::Options
25+
{
26+
struct filter { std::vector<std::string> include, exclude; };
27+
filter namespaces, files, entities;
28+
29+
std::string source_root;
30+
std::vector<std::string> input_include;
31+
};
32+
33+
} // mrdox
34+
} // clang
35+
2136
#if 0
2237
template<>
2338
struct llvm::yaml::MappingTraits<
24-
clang::mrdox::Config::filter>
39+
clang::mrdox::Config::Options::filter>
2540
{
2641
static void mapping(
27-
IO &io, clang::mrdox::Config::filter& f)
42+
IO &io, clang::mrdox::Config::Options::filter& f)
2843
{
2944
io.mapOptional("exclude", f);
3045
}
@@ -33,20 +48,13 @@ struct llvm::yaml::MappingTraits<
3348

3449
template<>
3550
struct llvm::yaml::MappingTraits<
36-
clang::mrdox::Config>
51+
clang::mrdox::Config::Options>
3752
{
3853
static void mapping(
39-
IO& io, clang::mrdox::Config& config)
54+
IO& io, clang::mrdox::Config::Options& opt)
4055
{
41-
#if 0
42-
io.mapOptional("namespaces", config.namespaces);
43-
io.mapOptional("files", config.files);
44-
io.mapOptional("entities", config.entities);
45-
io.mapOptional("project-name", config.ProjectName);
46-
io.mapOptional("public-only", config.PublicOnly);
47-
io.mapOptional("output-dir", config.OutDirectory);
48-
#endif
49-
io.mapOptional("source-root", config.sourceRoot_);
56+
io.mapOptional("source-root", opt.source_root);
57+
io.mapOptional("input", opt.input_include);
5058
}
5159
};
5260

@@ -55,6 +63,70 @@ struct llvm::yaml::MappingTraits<
5563
namespace clang {
5664
namespace mrdox {
5765

66+
Config::
67+
Config(
68+
llvm::StringRef configDir)
69+
: configDir_(configDir)
70+
{
71+
}
72+
73+
llvm::Expected<std::unique_ptr<Config>>
74+
Config::
75+
createAtDirectory(
76+
llvm::StringRef dirPath)
77+
{
78+
namespace fs = llvm::sys::fs;
79+
namespace path = llvm::sys::path;
80+
81+
llvm::SmallString<0> s(dirPath);
82+
std::error_code ec = fs::make_absolute(s);
83+
if(ec)
84+
return makeError("fs::make_absolute('", s, "') returned ", ec.message());
85+
path::remove_dots(s, true);
86+
makeDirsy(s);
87+
convert_to_slash(s);
88+
return std::unique_ptr<Config>(new Config(s));
89+
}
90+
91+
llvm::Expected<std::unique_ptr<Config>>
92+
Config::
93+
loadFromFile(
94+
llvm::StringRef filePath)
95+
{
96+
namespace fs = llvm::sys::fs;
97+
namespace path = llvm::sys::path;
98+
99+
// check filePath is a regular file
100+
fs::file_status stat;
101+
if(auto ec = fs::status(filePath, stat))
102+
return makeError("fs::status('", filePath, "') returned ", ec.message());
103+
if(stat.type() != fs::file_type::regular_file)
104+
return makeError("path '", filePath, "' is not a regular file");
105+
106+
// create initial config at config dir
107+
llvm::SmallString<0> s(filePath);
108+
path::remove_filename(s);
109+
llvm::Expected<std::unique_ptr<Config>> config = createAtDirectory(s);
110+
111+
// Read the YAML file into Options
112+
Options opt;
113+
auto fileText = llvm::MemoryBuffer::getFile(filePath);
114+
if(! fileText)
115+
return makeError(fileText.getError().message(), " when loading file '", filePath, "' ");
116+
{
117+
llvm::yaml::Input yin(**fileText);
118+
yin >> opt;
119+
if(auto ec = yin.error())
120+
return makeError(ec.message(), " when parsing file '", filePath, "' ");
121+
}
122+
123+
// apply opt to Config
124+
if(auto err = (*config)->setSourceRoot(opt.source_root))
125+
return err;
126+
127+
return config;
128+
}
129+
58130
//------------------------------------------------
59131
//
60132
// Observers
@@ -63,7 +135,7 @@ namespace mrdox {
63135

64136
bool
65137
Config::
66-
filterSourceFile(
138+
shouldVisitFile(
67139
llvm::StringRef filePath,
68140
llvm::SmallVectorImpl<char>& prefixPath) const noexcept
69141
{
@@ -72,11 +144,11 @@ filterSourceFile(
72144
llvm::SmallString<32> temp;
73145
temp = filePath;
74146
if(! path::replace_path_prefix(temp, sourceRoot_, ""))
75-
return true;
147+
return false;
76148

77149
prefixPath.assign(sourceRoot_.begin(), sourceRoot_.end());
78150
makeDirsy(prefixPath);
79-
return false;
151+
return true;
80152
}
81153

82154
//------------------------------------------------
@@ -101,56 +173,29 @@ setSourceRoot(
101173
return makeError("fs::make_absolute got '", ec, "'");
102174
}
103175

104-
path::remove_dots(temp, true);
176+
path::remove_dots(temp, true, path::Style::posix);
105177

106178
// This is required for filterSourceFile to work
107-
makeDirsy(temp);
179+
makeDirsy(temp, path::Style::posix);
108180
sourceRoot_ = temp.str();
109181

110182
return llvm::Error::success();
111183
}
112184

113-
//------------------------------------------------
114-
115-
bool
185+
llvm::Error
116186
Config::
117-
loadFromFile(
118-
llvm::StringRef filePath,
119-
Reporter& R)
187+
setInputFileFilter(
188+
std::vector<std::string> const& list)
120189
{
121-
namespace fs = llvm::sys::fs;
122190
namespace path = llvm::sys::path;
123191

124-
// get absolute path to config file
125-
configPath = filePath;
126-
fs::make_absolute(configPath);
127-
128-
// Read the YAML file and apply to this
129-
auto fileText = llvm::MemoryBuffer::getFile(configPath);
130-
if(R.error(fileText, "read the file '", configPath, "'"))
131-
return false;
132-
llvm::yaml::Input yin(**fileText);
133-
yin >> *this;
134-
if(R.error(yin.error(), "parse the YAML file"))
135-
return false;
136-
137-
// change configPath to the directory
138-
path::remove_filename(configPath);
139-
path::remove_dots(configPath, true);
140-
141-
// make sourceRoot absolute
142-
if(! path::is_absolute(sourceRoot_))
192+
for(auto const& s0 : list)
143193
{
144-
llvm::SmallString<128> temp;
145-
path::append(temp, configPath, sourceRoot_);
146-
// separator at the end is required
147-
// for replace_path_prefix to work
148-
path::remove_dots(temp, true);
149-
makeDirsy(temp);
150-
sourceRoot_.assign(temp.begin(), temp.end());
194+
std::string s;
195+
if(path::is_absolute(s0))
196+
s = path::convert_to_slash(s0);
151197
}
152-
153-
return true;
198+
return llvm::Error::success();
154199
}
155200

156201
} // mrdox

source/lib/Error.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ char ErrorInfoPlus::ID{};
6161

6262
llvm::Error
6363
makeErrorString(
64-
std::string action,
64+
std::string reason,
6565
std::source_location loc)
6666
{
67-
return llvm::make_error<ErrorInfoPlus>(std::move(action), loc);
67+
return llvm::make_error<ErrorInfoPlus>(std::move(reason), loc);
6868
}
6969

7070
} // mrdox

0 commit comments

Comments
 (0)