19
19
#include < llvm/Support/PrettyStackTrace.h>
20
20
#include < llvm/Support/Signals.h>
21
21
#include < cstdlib>
22
+ #include < ranges>
22
23
23
24
extern int main (int argc, char const ** argv);
24
25
25
- namespace clang {
26
- namespace mrdocs {
26
+ namespace clang ::mrdocs {
27
27
28
28
extern
29
29
int
@@ -47,7 +47,7 @@ print_version(llvm::raw_ostream& os)
47
47
<< " \n " ;
48
48
}
49
49
50
- Expected<std::pair<std::string, ReferenceDirectories> >
50
+ Expected<ReferenceDirectories>
51
51
getReferenceDirectories (std::string const & execPath)
52
52
{
53
53
ReferenceDirectories dirs;
@@ -58,36 +58,37 @@ getReferenceDirectories(std::string const& execPath)
58
58
return Unexpected (formatError (" Unable to determine current working directory: {}" , ec.message ()));
59
59
}
60
60
dirs.cwd = std::string (cwd.data (), cwd.size ());
61
+ return dirs;
62
+ }
63
+
64
+ Expected<std::string>
65
+ getConfigPath (ReferenceDirectories const & dirs)
66
+ {
61
67
std::string configPath;
62
- if (toolArgs.config .getValue () != " " )
68
+ auto cmdLineFilenames = std::ranges::views::transform (
69
+ toolArgs.cmdLineInputs , files::getFileName);
70
+ if (!toolArgs.config .getValue ().empty ())
63
71
{
72
+ // From explicit --config argument
64
73
configPath = toolArgs.config .getValue ();
65
74
}
66
- else
75
+ else if (auto const it = std::ranges::find (cmdLineFilenames, " mrdocs.yml" );
76
+ it != cmdLineFilenames.end ())
67
77
{
68
- llvm::cl::list<std::string>& inputs = toolArgs.cmdLineInputs ;
69
- for (auto & input: inputs)
70
- {
71
- if (files::getFileName (input) == " mrdocs.yml" )
72
- {
73
- configPath = input;
74
- break ;
75
- }
76
- }
78
+ // From implicit command line inputs
79
+ configPath = *(it.base ());
77
80
}
78
- if (configPath. empty ( ))
81
+ else if (files::exists ( " ./mrdocs.yml " ))
79
82
{
80
- if (files::exists (" ./mrdocs.yml" ))
81
- {
82
- configPath = " ./mrdocs.yml" ;
83
- }
83
+ // From current directory
84
+ configPath = " ./mrdocs.yml" ;
84
85
}
85
- if (configPath. empty ())
86
+ else
86
87
{
87
88
return Unexpected (formatError (" The config path is missing" ));
88
89
}
89
90
configPath = files::makeAbsolute (configPath, dirs.cwd );
90
- return std::make_pair ( configPath, dirs) ;
91
+ return configPath;
91
92
}
92
93
93
94
int
@@ -129,7 +130,15 @@ mrdocs_main(int argc, char const** argv)
129
130
report::fatal (" Failed to determine reference directories: {}" , res.error ().message ());
130
131
return EXIT_FAILURE;
131
132
}
132
- auto [configPath, dirs] = *res;
133
+ auto dirs = *std::move (res);
134
+
135
+ auto expConfigPath = getConfigPath (dirs);
136
+ if (!expConfigPath)
137
+ {
138
+ report::fatal (" Failed to determine config path: {}" , expConfigPath.error ().message ());
139
+ return EXIT_FAILURE;
140
+ }
141
+ auto configPath = *std::move (expConfigPath);
133
142
134
143
// Generate
135
144
auto exp = DoGenerateAction (configPath, dirs, argv);
@@ -156,8 +165,7 @@ reportUnhandledException(
156
165
sys::PrintStackTrace (llvm::errs ());
157
166
}
158
167
159
- } // mrdocs
160
- } // clang
168
+ } // clang::mrdocs
161
169
162
170
int
163
171
main (int argc, char const ** argv)
0 commit comments