Skip to content

[clang-tidy] fix incorrect configuration file path resolving when file paths contain .. #121323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 11, 2025
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
32 changes: 21 additions & 11 deletions clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBufferRef.h"
#include "llvm/Support/Path.h"
Expand Down Expand Up @@ -298,12 +299,11 @@ ConfigOptionsProvider::getRawOptions(llvm::StringRef FileName) {
if (ConfigOptions.InheritParentConfig.value_or(false)) {
LLVM_DEBUG(llvm::dbgs()
<< "Getting options for file " << FileName << "...\n");
assert(FS && "FS must be set.");

llvm::SmallString<128> AbsoluteFilePath(FileName);

if (!FS->makeAbsolute(AbsoluteFilePath)) {
addRawFileOptions(AbsoluteFilePath, RawOptions);
llvm::ErrorOr<llvm::SmallString<128>> AbsoluteFilePath =
getNormalizedAbsolutePath(FileName);
if (AbsoluteFilePath) {
addRawFileOptions(AbsoluteFilePath->str(), RawOptions);
}
}
RawOptions.emplace_back(ConfigOptions,
Expand Down Expand Up @@ -334,6 +334,17 @@ FileOptionsBaseProvider::FileOptionsBaseProvider(
OverrideOptions(std::move(OverrideOptions)),
ConfigHandlers(std::move(ConfigHandlers)) {}

llvm::ErrorOr<llvm::SmallString<128>>
FileOptionsBaseProvider::getNormalizedAbsolutePath(llvm::StringRef Path) {
assert(FS && "FS must be set.");
llvm::SmallString<128> NormalizedAbsolutePath = {Path};
std::error_code Err = FS->makeAbsolute(NormalizedAbsolutePath);
if (Err)
return Err;
llvm::sys::path::remove_dots(NormalizedAbsolutePath, /*remove_dot_dot=*/true);
return NormalizedAbsolutePath;
}

void FileOptionsBaseProvider::addRawFileOptions(
llvm::StringRef AbsolutePath, std::vector<OptionsSource> &CurOptions) {
auto CurSize = CurOptions.size();
Expand Down Expand Up @@ -396,16 +407,15 @@ std::vector<OptionsSource>
FileOptionsProvider::getRawOptions(StringRef FileName) {
LLVM_DEBUG(llvm::dbgs() << "Getting options for file " << FileName
<< "...\n");
assert(FS && "FS must be set.");

llvm::SmallString<128> AbsoluteFilePath(FileName);

if (FS->makeAbsolute(AbsoluteFilePath))
llvm::ErrorOr<llvm::SmallString<128>> AbsoluteFilePath =
getNormalizedAbsolutePath(FileName);
if (!AbsoluteFilePath)
return {};

std::vector<OptionsSource> RawOptions =
DefaultOptionsProvider::getRawOptions(AbsoluteFilePath.str());
addRawFileOptions(AbsoluteFilePath, RawOptions);
DefaultOptionsProvider::getRawOptions(AbsoluteFilePath->str());
addRawFileOptions(AbsoluteFilePath->str(), RawOptions);
OptionsSource CommandLineOptions(OverrideOptions,
OptionsSourceTypeCheckCommandLineOption);

Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidyOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H

#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/ErrorOr.h"
Expand Down Expand Up @@ -237,6 +238,9 @@ class FileOptionsBaseProvider : public DefaultOptionsProvider {
void addRawFileOptions(llvm::StringRef AbsolutePath,
std::vector<OptionsSource> &CurOptions);

llvm::ErrorOr<llvm::SmallString<128>>
getNormalizedAbsolutePath(llvm::StringRef AbsolutePath);

/// Try to read configuration files from \p Directory using registered
/// \c ConfigHandlers.
std::optional<OptionsSource> tryReadConfigFile(llvm::StringRef Directory);
Expand Down
3 changes: 3 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ Improvements to clang-tidy

- Improved :program:`clang-tidy` by accepting parameters file in command line.

- Improved :program:`clang-tidy` by fixing incorrect configuration file path
Copy link
Member

Choose a reason for hiding this comment

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

Note: to be honest all those 'Improved' release notes for same program could be merged together

Copy link
Contributor Author

Choose a reason for hiding this comment

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

resolving when file paths contain ``..``.

- Removed :program:`clang-tidy`'s global options for most of checks. All options
are changed to local options except `IncludeStyle`, `StrictMode` and
`IgnoreMacros`. Global scoped `StrictMode` and `IgnoreMacros` are deprecated
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
InvalidYamlFormat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// RUN: clang-tidy %S/Inputs/normalized-path/error-config/../code.cpp --verify-config 2>&1 | FileCheck %s

// CHECK-NOT: Error parsing
Loading