@@ -108,25 +108,10 @@ llvm::SmallVector<llvm::StringRef, 4> driver_invocation_argv(llvm::StringRef dri
108108}
109109
110110using QueryDriverError = CompilationDatabase::QueryDriverError;
111- using ErrorKind = CompilationDatabase::QueryDriverErrorKind;
112-
113- auto unexpected (ErrorKind kind,
114- llvm::StringRef driver,
115- std::string&& message,
116- std::optional<std::string> output_file = std::nullopt )
117- -> std::expected<CompilationDatabase::DriverInfo, QueryDriverError> {
118- QueryDriverError err{kind,
119- driver.str (),
120- std::forward<std::string>(message),
121- std::move (output_file)};
122- return std::unexpected (std::move (err));
123- };
111+ using ErrorKind = CompilationDatabase::QueryDriverError::ErrorKind;
124112
125- template <typename T>
126- auto unexpected (ErrorKind kind, llvm::StringRef driver, const llvm::ErrorOr<T>& error)
127- -> std::expected<CompilationDatabase::DriverInfo, QueryDriverError> {
128- QueryDriverError err{kind, driver.str (), std::format (" {}" , error.getError ())};
129- return std::unexpected (std::move (err));
113+ auto unexpected (ErrorKind kind, std::string message) {
114+ return std::unexpected<QueryDriverError>({kind, std::move (message)});
130115};
131116
132117} // namespace
@@ -139,7 +124,7 @@ auto CompilationDatabase::query_driver(this Self& self, llvm::StringRef driver)
139124 if (auto error = fs::real_path (driver, absolute_path)) {
140125 auto result = llvm::sys::findProgramByName (driver);
141126 if (!result) {
142- return unexpected (ErrorKind::NotFoundInPATH, driver, result);
127+ return unexpected (ErrorKind::NotFoundInPATH, result. getError (). message () );
143128 }
144129 absolute_path = *result;
145130 }
@@ -156,7 +141,7 @@ auto CompilationDatabase::query_driver(this Self& self, llvm::StringRef driver)
156141
157142 llvm::SmallString<128 > output_path;
158143 if (auto error = llvm::sys::fs::createTemporaryFile (" system-includes" , " clice" , output_path)) {
159- return unexpected (ErrorKind::FailToCreateTempFile, driver, error.message ());
144+ return unexpected (ErrorKind::FailToCreateTempFile, error.message ());
160145 }
161146
162147 // If we fail to get the driver infomation, keep the output file for user to debug.
@@ -184,12 +169,12 @@ auto CompilationDatabase::query_driver(this Self& self, llvm::StringRef driver)
184169 /* SecondsToWait=*/ 0 ,
185170 /* MemoryLimit=*/ 0 ,
186171 &message)) {
187- return unexpected (ErrorKind::InvokeDriverFail, driver, std::move (message));
172+ return unexpected (ErrorKind::InvokeDriverFail, std::move (message));
188173 }
189174
190175 auto file = llvm::MemoryBuffer::getFile (output_path);
191176 if (!file) {
192- return unexpected (ErrorKind::OutputFileNotReadable, driver, file.getError ().message ());
177+ return unexpected (ErrorKind::OutputFileNotReadable, file.getError ().message ());
193178 }
194179
195180 llvm::StringRef content = file.get ()->getBuffer ();
@@ -235,17 +220,11 @@ auto CompilationDatabase::query_driver(this Self& self, llvm::StringRef driver)
235220 }
236221
237222 if (!found_start_marker) {
238- return unexpected (ErrorKind::InvalidOutputFormat,
239- driver,
240- " Start marker not found..." ,
241- output_path.str ().str ());
223+ return unexpected (ErrorKind::InvalidOutputFormat, " Start marker not found..." );
242224 }
243225
244226 if (in_includes_block) {
245- return unexpected (ErrorKind::InvalidOutputFormat,
246- driver,
247- " End marker not found..." ,
248- output_path.str ().str ());
227+ return unexpected (ErrorKind::InvalidOutputFormat, " End marker not found..." );
249228 }
250229
251230 // Get driver information success, remove temporary file.
@@ -468,45 +447,10 @@ auto CompilationDatabase::load_commands(this Self& self, llvm::StringRef json_co
468447 return infos;
469448}
470449
471- namespace {
472-
473- // / Show warn and optional hint message for query driver error in log.
474- void handle_query_driver_error (QueryDriverError error) {
475- log::warn (" Failed to query driver info: {}" , error);
476-
477- llvm::SmallString<256 > hint;
478- llvm::raw_svector_ostream os (hint);
479- if (error.kind == ErrorKind::NotFoundInPATH) {
480- os << " Make sure that " << error.driver
481- << " has been installed in your PATH, or update the compile_commands.json to use correct driver." ;
482- } else if (error.kind == ErrorKind::InvokeDriverFail) {
483- os << " The failed invocation command is: " << error.driver ;
484- for (llvm::StringRef arg: driver_invocation_argv (error.driver )) {
485- os << ' ' << arg;
486- }
487- } else if (error.kind == ErrorKind::InvalidOutputFormat) {
488- os << " Check the output format of output file: " << error.output_file .value_or (" <unknown>" )
489- << " , for GCC/Clang it should be like:\n\n "
490- << " Target: <target>\n "
491- << " #include <...> search starts here:\n "
492- << " <system includes>\n "
493- << " End of search list." ;
494- }
495-
496- if (!hint.empty ()) {
497- log::warn (" HINT: {}" , hint);
498- }
499- }
500-
501- } // namespace
502-
503- auto CompilationDatabase::get_command (this Self& self,
504- llvm::StringRef file,
505- bool resource_dir,
506- bool query_driver) -> LookupInfo {
450+ auto CompilationDatabase::get_command (this Self& self, CommandOptions options) -> LookupInfo {
507451 LookupInfo info;
508452
509- file = self.save_string (file);
453+ llvm::StringRef file = self.save_string (options. file );
510454 auto it = self.command_infos .find (file.data ());
511455 if (it != self.command_infos .end ()) {
512456 info.dictionary = it->second .dictionary ;
@@ -524,7 +468,7 @@ auto CompilationDatabase::get_command(this Self& self,
524468 info.arguments .emplace_back (self.save_string (argument).data ());
525469 };
526470
527- if (query_driver) {
471+ if (options. query_driver ) {
528472 llvm::StringRef driver = info.arguments [0 ];
529473 if (auto driver_info = self.query_driver (driver)) {
530474 append_argument (" -nostdlibinc" );
@@ -536,14 +480,12 @@ auto CompilationDatabase::get_command(this Self& self,
536480 append_argument (" -I" );
537481 append_argument (system_header);
538482 }
539- } else {
540- // / FIXME: Show log in `handle_query_driver_error` will cause many unsed output in
541- // / unittest. We should find a way to suppress this output in unittest.
542- handle_query_driver_error (std::move (driver_info).error ());
483+ } else if (!options.suppress_log ) {
484+ log::warn (" Failed to query driver:{}, error:{}" , driver, driver_info.error ());
543485 }
544486 }
545487
546- if (resource_dir) {
488+ if (options. resource_dir ) {
547489 append_argument (std::format (" -resource-dir={}" , fs::resource_dir));
548490 }
549491
0 commit comments