@@ -337,33 +337,34 @@ FileOptionsBaseProvider::FileOptionsBaseProvider(
337337void FileOptionsBaseProvider::addRawFileOptions (
338338 llvm::StringRef AbsolutePath, std::vector<OptionsSource> &CurOptions) {
339339 auto CurSize = CurOptions.size ();
340-
341340 // Look for a suitable configuration file in all parent directories of the
342341 // file. Start with the immediate parent directory and move up.
343- StringRef Path = llvm::sys::path::parent_path (AbsolutePath);
344- for (StringRef CurrentPath = Path; !CurrentPath.empty ();
345- CurrentPath = llvm::sys::path::parent_path (CurrentPath)) {
346- std::optional<OptionsSource> Result;
347-
348- auto Iter = CachedOptions.find (CurrentPath);
349- if (Iter != CachedOptions.end ())
350- Result = Iter->second ;
351-
352- if (!Result)
353- Result = tryReadConfigFile (CurrentPath);
354-
355- if (Result) {
356- // Store cached value for all intermediate directories.
357- while (Path != CurrentPath) {
342+ StringRef RootPath = llvm::sys::path::parent_path (AbsolutePath);
343+ auto MemorizedConfigFile =
344+ [this , &RootPath](StringRef CurrentPath) -> std::optional<OptionsSource> {
345+ const auto Iter = CachedOptions.Memorized .find (CurrentPath);
346+ if (Iter != CachedOptions.Memorized .end ())
347+ return CachedOptions.Storage [Iter->second ];
348+ std::optional<OptionsSource> OptionsSource = tryReadConfigFile (CurrentPath);
349+ if (OptionsSource) {
350+ const size_t Index = CachedOptions.Storage .size ();
351+ CachedOptions.Storage .emplace_back (OptionsSource.value ());
352+ while (RootPath != CurrentPath) {
358353 LLVM_DEBUG (llvm::dbgs ()
359- << " Caching configuration for path " << Path << " .\n " );
360- if (!CachedOptions.count (Path))
361- CachedOptions[Path] = *Result;
362- Path = llvm::sys::path::parent_path (Path);
354+ << " Caching configuration for path " << RootPath << " .\n " );
355+ CachedOptions.Memorized [RootPath] = Index;
356+ RootPath = llvm::sys::path::parent_path (RootPath);
363357 }
364- CachedOptions[Path] = *Result;
365-
366- CurOptions.push_back (*Result);
358+ CachedOptions.Memorized [CurrentPath] = Index;
359+ RootPath = llvm::sys::path::parent_path (CurrentPath);
360+ }
361+ return OptionsSource;
362+ };
363+ for (StringRef CurrentPath = RootPath; !CurrentPath.empty ();
364+ CurrentPath = llvm::sys::path::parent_path (CurrentPath)) {
365+ if (std::optional<OptionsSource> Result =
366+ MemorizedConfigFile (CurrentPath)) {
367+ CurOptions.emplace_back (Result.value ());
367368 if (!Result->first .InheritParentConfig .value_or (false ))
368369 break ;
369370 }
0 commit comments