1919#include < llvm/Support/PrettyStackTrace.h>
2020#include < llvm/Support/Signals.h>
2121#include < cstdlib>
22+ #include < ranges>
2223
2324extern int main (int argc, char const ** argv);
2425
25- namespace clang {
26- namespace mrdocs {
26+ namespace clang ::mrdocs {
2727
2828extern
2929int
@@ -47,7 +47,7 @@ print_version(llvm::raw_ostream& os)
4747 << " \n " ;
4848}
4949
50- Expected<std::pair<std::string, ReferenceDirectories> >
50+ Expected<ReferenceDirectories>
5151getReferenceDirectories (std::string const & execPath)
5252{
5353 ReferenceDirectories dirs;
@@ -58,36 +58,37 @@ getReferenceDirectories(std::string const& execPath)
5858 return Unexpected (formatError (" Unable to determine current working directory: {}" , ec.message ()));
5959 }
6060 dirs.cwd = std::string (cwd.data (), cwd.size ());
61+ return dirs;
62+ }
63+
64+ Expected<std::string>
65+ getConfigPath (ReferenceDirectories const & dirs)
66+ {
6167 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 ())
6371 {
72+ // From explicit --config argument
6473 configPath = toolArgs.config .getValue ();
6574 }
66- else
75+ else if (auto const it = std::ranges::find (cmdLineFilenames, " mrdocs.yml" );
76+ it != cmdLineFilenames.end ())
6777 {
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 ());
7780 }
78- if (configPath. empty ( ))
81+ else if (files::exists ( " ./mrdocs.yml " ))
7982 {
80- if (files::exists (" ./mrdocs.yml" ))
81- {
82- configPath = " ./mrdocs.yml" ;
83- }
83+ // From current directory
84+ configPath = " ./mrdocs.yml" ;
8485 }
85- if (configPath. empty ())
86+ else
8687 {
8788 return Unexpected (formatError (" The config path is missing" ));
8889 }
8990 configPath = files::makeAbsolute (configPath, dirs.cwd );
90- return std::make_pair ( configPath, dirs) ;
91+ return configPath;
9192}
9293
9394int
@@ -129,7 +130,15 @@ mrdocs_main(int argc, char const** argv)
129130 report::fatal (" Failed to determine reference directories: {}" , res.error ().message ());
130131 return EXIT_FAILURE;
131132 }
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);
133142
134143 // Generate
135144 auto exp = DoGenerateAction (configPath, dirs, argv);
@@ -156,8 +165,7 @@ reportUnhandledException(
156165 sys::PrintStackTrace (llvm::errs ());
157166}
158167
159- } // mrdocs
160- } // clang
168+ } // clang::mrdocs
161169
162170int
163171main (int argc, char const ** argv)
0 commit comments