Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,33 +337,34 @@ FileOptionsBaseProvider::FileOptionsBaseProvider(
void FileOptionsBaseProvider::addRawFileOptions(
llvm::StringRef AbsolutePath, std::vector<OptionsSource> &CurOptions) {
auto CurSize = CurOptions.size();

// Look for a suitable configuration file in all parent directories of the
// file. Start with the immediate parent directory and move up.
StringRef Path = llvm::sys::path::parent_path(AbsolutePath);
for (StringRef CurrentPath = Path; !CurrentPath.empty();
CurrentPath = llvm::sys::path::parent_path(CurrentPath)) {
std::optional<OptionsSource> Result;

auto Iter = CachedOptions.find(CurrentPath);
if (Iter != CachedOptions.end())
Result = Iter->second;

if (!Result)
Result = tryReadConfigFile(CurrentPath);

if (Result) {
// Store cached value for all intermediate directories.
while (Path != CurrentPath) {
StringRef RootPath = llvm::sys::path::parent_path(AbsolutePath);
auto MemorizedConfigFile =
[this, &RootPath](StringRef CurrentPath) -> std::optional<OptionsSource> {
auto Iter = CachedOptions.Memorized.find(CurrentPath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can be const, and can be declared as the if-init var to reduce its scope

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be declared as the if-init var to reduce its scope

Do you mean something like if (auto it = m.find(10); it != m.end())? I do not think it is a common code style in clang.

if (Iter != CachedOptions.Memorized.end())
return CachedOptions.Storage[Iter->second];
std::optional<OptionsSource> OptionsSource = tryReadConfigFile(CurrentPath);
if (OptionsSource) {
const size_t Index = CachedOptions.Storage.size();
CachedOptions.Storage.emplace_back(OptionsSource.value());
while (RootPath != CurrentPath) {
LLVM_DEBUG(llvm::dbgs()
<< "Caching configuration for path " << Path << ".\n");
if (!CachedOptions.count(Path))
CachedOptions[Path] = *Result;
Path = llvm::sys::path::parent_path(Path);
<< "Caching configuration for path " << RootPath << ".\n");
CachedOptions.Memorized[RootPath] = Index;
RootPath = llvm::sys::path::parent_path(RootPath);
}
CachedOptions[Path] = *Result;

CurOptions.push_back(*Result);
CachedOptions.Memorized[CurrentPath] = Index;
RootPath = llvm::sys::path::parent_path(CurrentPath);
}
return OptionsSource;
};
for (StringRef CurrentPath = RootPath; !CurrentPath.empty();
CurrentPath = llvm::sys::path::parent_path(CurrentPath)) {
if (std::optional<OptionsSource> Result =
MemorizedConfigFile(CurrentPath)) {
CurOptions.emplace_back(Result.value());
if (!Result->first.InheritParentConfig.value_or(false))
break;
}
Expand Down
5 changes: 4 additions & 1 deletion clang-tools-extra/clang-tidy/ClangTidyOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ class FileOptionsBaseProvider : public DefaultOptionsProvider {
/// \c ConfigHandlers.
std::optional<OptionsSource> tryReadConfigFile(llvm::StringRef Directory);

llvm::StringMap<OptionsSource> CachedOptions;
struct OptionsCache {
llvm::StringMap<size_t> Memorized;
llvm::SmallVector<OptionsSource, 4U> Storage;
} CachedOptions;
ClangTidyOptions OverrideOptions;
ConfigFileHandlers ConfigHandlers;
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
Expand Down
Loading