Skip to content

Commit 26993f6

Browse files
committed
Revert "[clang-format] Optimize processing .clang-format-ignore files (#76733)"
This reverts commit 42ec976. Reason: Broke the sanitizer buildbots. See more information on the github comment thread at 42ec976
1 parent b4ac4d2 commit 26993f6

File tree

3 files changed

+27
-69
lines changed

3 files changed

+27
-69
lines changed

clang/docs/ClangFormat.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,6 @@ An easy way to create the ``.clang-format`` file is:
131131
132132
Available style options are described in :doc:`ClangFormatStyleOptions`.
133133

134-
.clang-format-ignore
135-
====================
136-
137134
You can create ``.clang-format-ignore`` files to make ``clang-format`` ignore
138135
certain files. A ``.clang-format-ignore`` file consists of patterns of file path
139136
names. It has the following format:
@@ -144,8 +141,7 @@ names. It has the following format:
144141
* A non-comment line is a single pattern.
145142
* The slash (``/``) is used as the directory separator.
146143
* A pattern is relative to the directory of the ``.clang-format-ignore`` file
147-
(or the root directory if the pattern starts with a slash). Patterns
148-
containing drive names (e.g. ``C:``) are not supported.
144+
(or the root directory if the pattern starts with a slash).
149145
* Patterns follow the rules specified in `POSIX 2.13.1, 2.13.2, and Rule 1 of
150146
2.13.3 <https://pubs.opengroup.org/onlinepubs/9699919799/utilities/
151147
V3_chap02.html#tag_18_13>`_.

clang/test/Format/clang-format-ignore.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,13 @@
2121

2222
// RUN: touch .clang-format-ignore
2323
// RUN: clang-format -verbose foo.c foo.js 2> %t.stderr
24-
// RUN: grep -Fx "Formatting [1/2] foo.c" %t.stderr
25-
// RUN: grep -Fx "Formatting [2/2] foo.js" %t.stderr
24+
// RUN: grep "Formatting \[1/2] foo.c" %t.stderr
25+
// RUN: grep "Formatting \[2/2] foo.js" %t.stderr
2626

2727
// RUN: echo "*.js" > .clang-format-ignore
2828
// RUN: clang-format -verbose foo.c foo.js 2> %t.stderr
29-
// RUN: grep -Fx "Formatting [1/2] foo.c" %t.stderr
30-
// RUN: not grep -F foo.js %t.stderr
29+
// RUN: grep "Formatting \[1/2] foo.c" %t.stderr
30+
// RUN: not grep "Formatting \[2/2] foo.js" %t.stderr
3131

32-
// RUN: cd ../..
33-
// RUN: clang-format -verbose *.cc level1/*.c* level1/level2/foo.* 2> %t.stderr
34-
// RUN: grep -x "Formatting \[1/5] .*foo\.c" %t.stderr
35-
// RUN: not grep -F foo.js %t.stderr
36-
37-
// RUN: rm .clang-format-ignore
38-
// RUN: clang-format -verbose *.cc level1/*.c* level1/level2/foo.* 2> %t.stderr
39-
// RUN: grep -x "Formatting \[1/5] .*foo\.cc" %t.stderr
40-
// RUN: grep -x "Formatting \[2/5] .*bar\.cc" %t.stderr
41-
// RUN: grep -x "Formatting \[3/5] .*baz\.c" %t.stderr
42-
// RUN: grep -x "Formatting \[4/5] .*foo\.c" %t.stderr
43-
// RUN: not grep -F foo.js %t.stderr
44-
45-
// RUN: cd ..
46-
// RUN: rm -r %t.dir
32+
// RUN: cd ../../..
33+
// RUN: rm -rf %t.dir

clang/tools/clang-format/ClangFormat.cpp

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -571,11 +571,6 @@ static int dumpConfig(bool IsSTDIN) {
571571
return 0;
572572
}
573573

574-
using String = SmallString<128>;
575-
static String IgnoreDir; // Directory of .clang-format-ignore file.
576-
static StringRef PrevDir; // Directory of previous `FilePath`.
577-
static SmallVector<String> Patterns; // Patterns in .clang-format-ignore file.
578-
579574
// Check whether `FilePath` is ignored according to the nearest
580575
// .clang-format-ignore file based on the rules below:
581576
// - A blank line is skipped.
@@ -591,65 +586,45 @@ static bool isIgnored(StringRef FilePath) {
591586
if (!is_regular_file(FilePath))
592587
return false;
593588

594-
String Path;
595-
String AbsPath{FilePath};
596-
597589
using namespace llvm::sys::path;
590+
SmallString<128> Path, AbsPath{FilePath};
591+
598592
make_absolute(AbsPath);
599593
remove_dots(AbsPath, /*remove_dot_dot=*/true);
600594

601-
if (StringRef Dir{parent_path(AbsPath)}; PrevDir != Dir) {
602-
PrevDir = Dir;
603-
604-
for (;;) {
605-
Path = Dir;
606-
append(Path, ".clang-format-ignore");
607-
if (is_regular_file(Path))
608-
break;
609-
Dir = parent_path(Dir);
610-
if (Dir.empty())
611-
return false;
612-
}
613-
614-
IgnoreDir = convert_to_slash(Dir);
615-
616-
std::ifstream IgnoreFile{Path.c_str()};
617-
if (!IgnoreFile.good())
595+
StringRef IgnoreDir{AbsPath};
596+
do {
597+
IgnoreDir = parent_path(IgnoreDir);
598+
if (IgnoreDir.empty())
618599
return false;
619600

620-
Patterns.clear();
601+
Path = IgnoreDir;
602+
append(Path, ".clang-format-ignore");
603+
} while (!is_regular_file(Path));
621604

622-
for (std::string Line; std::getline(IgnoreFile, Line);) {
623-
if (const auto Pattern{StringRef{Line}.trim()};
624-
// Skip empty and comment lines.
625-
!Pattern.empty() && Pattern[0] != '#') {
626-
Patterns.push_back(Pattern);
627-
}
628-
}
629-
}
630-
631-
if (IgnoreDir.empty())
605+
std::ifstream IgnoreFile{Path.c_str()};
606+
if (!IgnoreFile.good())
632607
return false;
633608

634-
const auto Pathname{convert_to_slash(AbsPath)};
635-
for (const auto &Pat : Patterns) {
636-
const bool IsNegated = Pat[0] == '!';
637-
StringRef Pattern{Pat};
609+
const auto Pathname = convert_to_slash(AbsPath);
610+
for (std::string Line; std::getline(IgnoreFile, Line);) {
611+
auto Pattern = StringRef(Line).trim();
612+
if (Pattern.empty() || Pattern[0] == '#')
613+
continue;
614+
615+
const bool IsNegated = Pattern[0] == '!';
638616
if (IsNegated)
639617
Pattern = Pattern.drop_front();
640618

641619
if (Pattern.empty())
642620
continue;
643621

644622
Pattern = Pattern.ltrim();
645-
646-
// `Pattern` is relative to `IgnoreDir` unless it starts with a slash.
647-
// This doesn't support patterns containing drive names (e.g. `C:`).
648623
if (Pattern[0] != '/') {
649-
Path = IgnoreDir;
624+
Path = convert_to_slash(IgnoreDir);
650625
append(Path, Style::posix, Pattern);
651626
remove_dots(Path, /*remove_dot_dot=*/true, Style::posix);
652-
Pattern = Path;
627+
Pattern = Path.str();
653628
}
654629

655630
if (clang::format::matchFilePath(Pattern, Pathname) == !IsNegated)

0 commit comments

Comments
 (0)