@@ -571,11 +571,6 @@ static int dumpConfig(bool IsSTDIN) {
571
571
return 0 ;
572
572
}
573
573
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
-
579
574
// Check whether `FilePath` is ignored according to the nearest
580
575
// .clang-format-ignore file based on the rules below:
581
576
// - A blank line is skipped.
@@ -591,65 +586,45 @@ static bool isIgnored(StringRef FilePath) {
591
586
if (!is_regular_file (FilePath))
592
587
return false ;
593
588
594
- String Path;
595
- String AbsPath{FilePath};
596
-
597
589
using namespace llvm ::sys::path;
590
+ SmallString<128 > Path, AbsPath{FilePath};
591
+
598
592
make_absolute (AbsPath);
599
593
remove_dots (AbsPath, /* remove_dot_dot=*/ true );
600
594
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 ())
618
599
return false ;
619
600
620
- Patterns.clear ();
601
+ Path = IgnoreDir;
602
+ append (Path, " .clang-format-ignore" );
603
+ } while (!is_regular_file (Path));
621
604
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 ())
632
607
return false ;
633
608
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 ] == ' !' ;
638
616
if (IsNegated)
639
617
Pattern = Pattern.drop_front ();
640
618
641
619
if (Pattern.empty ())
642
620
continue ;
643
621
644
622
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:`).
648
623
if (Pattern[0 ] != ' /' ) {
649
- Path = IgnoreDir;
624
+ Path = convert_to_slash ( IgnoreDir) ;
650
625
append (Path, Style::posix, Pattern);
651
626
remove_dots (Path, /* remove_dot_dot=*/ true , Style::posix);
652
- Pattern = Path;
627
+ Pattern = Path. str () ;
653
628
}
654
629
655
630
if (clang::format::matchFilePath (Pattern, Pathname) == !IsNegated)
0 commit comments