clang-tools  17.0.0git
FileExtensionsUtils.cpp
Go to the documentation of this file.
1 //===--- FileExtensionsUtils.cpp - clang-tidy -------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "FileExtensionsUtils.h"
10 #include "clang/Basic/CharInfo.h"
11 #include "llvm/Support/Path.h"
12 #include <optional>
13 
14 namespace clang::tidy::utils {
15 
16 bool isExpansionLocInHeaderFile(SourceLocation Loc, const SourceManager &SM,
17  const FileExtensionsSet &HeaderFileExtensions) {
18  SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
19  return isFileExtension(SM.getFilename(ExpansionLoc), HeaderFileExtensions);
20 }
21 
22 bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM,
23  const FileExtensionsSet &HeaderFileExtensions) {
24  PresumedLoc PresumedLocation = SM.getPresumedLoc(Loc);
25  return isFileExtension(PresumedLocation.getFilename(), HeaderFileExtensions);
26 }
27 
28 bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM,
29  const FileExtensionsSet &HeaderFileExtensions) {
30  SourceLocation SpellingLoc = SM.getSpellingLoc(Loc);
31  return isFileExtension(SM.getFilename(SpellingLoc), HeaderFileExtensions);
32 }
33 
34 bool parseFileExtensions(StringRef AllFileExtensions,
35  FileExtensionsSet &FileExtensions,
36  StringRef Delimiters) {
37  SmallVector<StringRef, 5> Suffixes;
38  for (char Delimiter : Delimiters) {
39  if (AllFileExtensions.contains(Delimiter)) {
40  AllFileExtensions.split(Suffixes, Delimiter);
41  break;
42  }
43  }
44 
45  FileExtensions.clear();
46  for (StringRef Suffix : Suffixes) {
47  StringRef Extension = Suffix.trim();
48  if (!llvm::all_of(Extension, isAlphanumeric))
49  return false;
50  FileExtensions.insert(Extension);
51  }
52  return true;
53 }
54 
55 std::optional<StringRef>
56 getFileExtension(StringRef FileName, const FileExtensionsSet &FileExtensions) {
57  StringRef Extension = llvm::sys::path::extension(FileName);
58  if (Extension.empty())
59  return std::nullopt;
60  // Skip "." prefix.
61  if (!FileExtensions.count(Extension.substr(1)))
62  return std::nullopt;
63  return Extension;
64 }
65 
66 bool isFileExtension(StringRef FileName,
67  const FileExtensionsSet &FileExtensions) {
68  return getFileExtension(FileName, FileExtensions).has_value();
69 }
70 
71 } // namespace clang::tidy::utils
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:43
Suffix
std::string Suffix
Definition: AddUsing.cpp:113
clang::tidy::utils::isPresumedLocInHeaderFile
bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether presumed location of Loc is in header file.
Definition: FileExtensionsUtils.cpp:22
clang::tidy::utils::parseFileExtensions
bool parseFileExtensions(StringRef AllFileExtensions, FileExtensionsSet &FileExtensions, StringRef Delimiters)
Parses header file extensions from a semicolon-separated list.
Definition: FileExtensionsUtils.cpp:34
clang::tidy::utils::isFileExtension
bool isFileExtension(StringRef FileName, const FileExtensionsSet &FileExtensions)
Decides whether a file has one of the specified file extensions.
Definition: FileExtensionsUtils.cpp:66
clang::tidy::utils::isSpellingLocInHeaderFile
bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether spelling location of Loc is in header file.
Definition: FileExtensionsUtils.cpp:28
Suffixes
static constexpr llvm::StringLiteral Suffixes
Definition: UppercaseLiteralSuffixCheck.cpp:31
FileName
StringRef FileName
Definition: KernelNameRestrictionCheck.cpp:44
FileExtensionsUtils.h
clang::tidy::utils
Definition: Aliasing.cpp:14
clang::tidy::utils::isExpansionLocInHeaderFile
bool isExpansionLocInHeaderFile(SourceLocation Loc, const SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether expansion location of Loc is in header file.
Definition: FileExtensionsUtils.cpp:16
clang::tidy::utils::getFileExtension
std::optional< StringRef > getFileExtension(StringRef FileName, const FileExtensionsSet &FileExtensions)
Decides whether a file has a header file extension.
Definition: FileExtensionsUtils.cpp:56
clang::tidy::utils::FileExtensionsSet
llvm::SmallSet< llvm::StringRef, 5 > FileExtensionsSet
Definition: FileExtensionsUtils.h:20