xref: /src/contrib/llvm-project/clang/lib/Format/MatchFilePath.cpp (revision 647cbc5de815c5651677bf8582797f716ec7b48d)
199aabd70SDimitry Andric //===--- MatchFilePath.cpp - Match file path with pattern -------*- C++ -*-===//
299aabd70SDimitry Andric //
399aabd70SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
499aabd70SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
599aabd70SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
699aabd70SDimitry Andric //
799aabd70SDimitry Andric //===----------------------------------------------------------------------===//
899aabd70SDimitry Andric ///
999aabd70SDimitry Andric /// \file
1099aabd70SDimitry Andric /// This file implements the functionality of matching a file path name to
1199aabd70SDimitry Andric /// a pattern, similar to the POSIX fnmatch() function.
1299aabd70SDimitry Andric ///
1399aabd70SDimitry Andric //===----------------------------------------------------------------------===//
1499aabd70SDimitry Andric 
1599aabd70SDimitry Andric #include "MatchFilePath.h"
1699aabd70SDimitry Andric 
1799aabd70SDimitry Andric using namespace llvm;
1899aabd70SDimitry Andric 
1999aabd70SDimitry Andric namespace clang {
2099aabd70SDimitry Andric namespace format {
2199aabd70SDimitry Andric 
2277dbea07SDimitry Andric // Check whether `FilePath` matches `Pattern` based on POSIX 2.13.1, 2.13.2, and
2377dbea07SDimitry Andric // Rule 1 of 2.13.3.
matchFilePath(StringRef Pattern,StringRef FilePath)2499aabd70SDimitry Andric bool matchFilePath(StringRef Pattern, StringRef FilePath) {
2599aabd70SDimitry Andric   assert(!Pattern.empty());
2699aabd70SDimitry Andric   assert(!FilePath.empty());
2799aabd70SDimitry Andric 
2899aabd70SDimitry Andric   // No match if `Pattern` ends with a non-meta character not equal to the last
2999aabd70SDimitry Andric   // character of `FilePath`.
3099aabd70SDimitry Andric   if (const auto C = Pattern.back(); !strchr("?*]", C) && C != FilePath.back())
3199aabd70SDimitry Andric     return false;
3299aabd70SDimitry Andric 
3399aabd70SDimitry Andric   constexpr auto Separator = '/';
3499aabd70SDimitry Andric   const auto EOP = Pattern.size();  // End of `Pattern`.
3599aabd70SDimitry Andric   const auto End = FilePath.size(); // End of `FilePath`.
3699aabd70SDimitry Andric   unsigned I = 0;                   // Index to `Pattern`.
3799aabd70SDimitry Andric 
3899aabd70SDimitry Andric   for (unsigned J = 0; J < End; ++J) {
3999aabd70SDimitry Andric     if (I == EOP)
4099aabd70SDimitry Andric       return false;
4199aabd70SDimitry Andric 
4299aabd70SDimitry Andric     switch (const auto F = FilePath[J]; Pattern[I]) {
4399aabd70SDimitry Andric     case '\\':
4499aabd70SDimitry Andric       if (++I == EOP || F != Pattern[I])
4599aabd70SDimitry Andric         return false;
4699aabd70SDimitry Andric       break;
4799aabd70SDimitry Andric     case '?':
4899aabd70SDimitry Andric       if (F == Separator)
4999aabd70SDimitry Andric         return false;
5099aabd70SDimitry Andric       break;
5199aabd70SDimitry Andric     case '*': {
5299aabd70SDimitry Andric       while (++I < EOP && Pattern[I] == '*') { // Skip consecutive stars.
5399aabd70SDimitry Andric       }
5499aabd70SDimitry Andric       const auto K = FilePath.find(Separator, J); // Index of next `Separator`.
5599aabd70SDimitry Andric       const bool NoMoreSeparatorsInFilePath = K == StringRef::npos;
5699aabd70SDimitry Andric       if (I == EOP) // `Pattern` ends with a star.
5799aabd70SDimitry Andric         return NoMoreSeparatorsInFilePath;
5899aabd70SDimitry Andric       // `Pattern` ends with a lone backslash.
5999aabd70SDimitry Andric       if (Pattern[I] == '\\' && ++I == EOP)
6099aabd70SDimitry Andric         return false;
6199aabd70SDimitry Andric       // The star is followed by a (possibly escaped) `Separator`.
6299aabd70SDimitry Andric       if (Pattern[I] == Separator) {
6399aabd70SDimitry Andric         if (NoMoreSeparatorsInFilePath)
6499aabd70SDimitry Andric           return false;
6599aabd70SDimitry Andric         J = K; // Skip to next `Separator` in `FilePath`.
6699aabd70SDimitry Andric         break;
6799aabd70SDimitry Andric       }
6899aabd70SDimitry Andric       // Recurse.
6999aabd70SDimitry Andric       for (auto Pat = Pattern.substr(I); J < End && FilePath[J] != Separator;
7099aabd70SDimitry Andric            ++J) {
7199aabd70SDimitry Andric         if (matchFilePath(Pat, FilePath.substr(J)))
7299aabd70SDimitry Andric           return true;
7399aabd70SDimitry Andric       }
7499aabd70SDimitry Andric       return false;
7599aabd70SDimitry Andric     }
7699aabd70SDimitry Andric     case '[':
7799aabd70SDimitry Andric       // Skip e.g. `[!]`.
7899aabd70SDimitry Andric       if (I + 3 < EOP || (I + 3 == EOP && Pattern[I + 1] != '!')) {
7999aabd70SDimitry Andric         // Skip unpaired `[`, brackets containing slashes, and `[]`.
8099aabd70SDimitry Andric         if (const auto K = Pattern.find_first_of("]/", I + 1);
8199aabd70SDimitry Andric             K != StringRef::npos && Pattern[K] == ']' && K > I + 1) {
8299aabd70SDimitry Andric           if (F == Separator)
8399aabd70SDimitry Andric             return false;
8499aabd70SDimitry Andric           ++I; // After the `[`.
8599aabd70SDimitry Andric           bool Negated = false;
8699aabd70SDimitry Andric           if (Pattern[I] == '!') {
8799aabd70SDimitry Andric             Negated = true;
8899aabd70SDimitry Andric             ++I; // After the `!`.
8999aabd70SDimitry Andric           }
9099aabd70SDimitry Andric           bool Match = false;
9199aabd70SDimitry Andric           do {
9299aabd70SDimitry Andric             if (I + 2 < K && Pattern[I + 1] == '-') {
9399aabd70SDimitry Andric               Match = Pattern[I] <= F && F <= Pattern[I + 2];
9499aabd70SDimitry Andric               I += 3; // After the range, e.g. `A-Z`.
9599aabd70SDimitry Andric             } else {
9699aabd70SDimitry Andric               Match = F == Pattern[I++];
9799aabd70SDimitry Andric             }
9899aabd70SDimitry Andric           } while (!Match && I < K);
9999aabd70SDimitry Andric           if (Negated ? Match : !Match)
10099aabd70SDimitry Andric             return false;
10199aabd70SDimitry Andric           I = K + 1; // After the `]`.
10299aabd70SDimitry Andric           continue;
10399aabd70SDimitry Andric         }
10499aabd70SDimitry Andric       }
10599aabd70SDimitry Andric       [[fallthrough]]; // Match `[` literally.
10699aabd70SDimitry Andric     default:
10799aabd70SDimitry Andric       if (F != Pattern[I])
10899aabd70SDimitry Andric         return false;
10999aabd70SDimitry Andric     }
11099aabd70SDimitry Andric 
11199aabd70SDimitry Andric     ++I;
11299aabd70SDimitry Andric   }
11399aabd70SDimitry Andric 
11499aabd70SDimitry Andric   // Match trailing stars with null strings.
11599aabd70SDimitry Andric   while (I < EOP && Pattern[I] == '*')
11699aabd70SDimitry Andric     ++I;
11799aabd70SDimitry Andric 
11899aabd70SDimitry Andric   return I == EOP;
11999aabd70SDimitry Andric }
12099aabd70SDimitry Andric 
12199aabd70SDimitry Andric } // namespace format
12299aabd70SDimitry Andric } // namespace clang
123