clang-tools  14.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 
13 namespace clang {
14 namespace tidy {
15 namespace utils {
16 
17 bool isExpansionLocInHeaderFile(SourceLocation Loc, const SourceManager &SM,
18  const FileExtensionsSet &HeaderFileExtensions) {
19  SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
20  return isFileExtension(SM.getFilename(ExpansionLoc), HeaderFileExtensions);
21 }
22 
23 bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM,
24  const FileExtensionsSet &HeaderFileExtensions) {
25  PresumedLoc PresumedLocation = SM.getPresumedLoc(Loc);
26  return isFileExtension(PresumedLocation.getFilename(), HeaderFileExtensions);
27 }
28 
29 bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM,
30  const FileExtensionsSet &HeaderFileExtensions) {
31  SourceLocation SpellingLoc = SM.getSpellingLoc(Loc);
32  return isFileExtension(SM.getFilename(SpellingLoc), HeaderFileExtensions);
33 }
34 
35 bool parseFileExtensions(StringRef AllFileExtensions,
36  FileExtensionsSet &FileExtensions,
37  StringRef Delimiters) {
38  SmallVector<StringRef, 5> Suffixes;
39  for (char Delimiter : Delimiters) {
40  if (AllFileExtensions.contains(Delimiter)) {
41  AllFileExtensions.split(Suffixes, Delimiter);
42  break;
43  }
44  }
45 
46  FileExtensions.clear();
47  for (StringRef Suffix : Suffixes) {
48  StringRef Extension = Suffix.trim();
49  if (!llvm::all_of(Extension, isAlphanumeric))
50  return false;
51  FileExtensions.insert(Extension);
52  }
53  return true;
54 }
55 
56 llvm::Optional<StringRef>
57 getFileExtension(StringRef FileName, const FileExtensionsSet &FileExtensions) {
58  StringRef Extension = llvm::sys::path::extension(FileName);
59  if (Extension.empty())
60  return llvm::None;
61  // Skip "." prefix.
62  if (!FileExtensions.count(Extension.substr(1)))
63  return llvm::None;
64  return Extension;
65 }
66 
67 bool isFileExtension(StringRef FileName,
68  const FileExtensionsSet &FileExtensions) {
69  return getFileExtension(FileName, FileExtensions).hasValue();
70 }
71 
72 } // namespace utils
73 } // namespace tidy
74 } // namespace clang
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
Suffix
std::string Suffix
Definition: AddUsing.cpp:112
clang::tidy::utils::getFileExtension
llvm::Optional< StringRef > getFileExtension(StringRef FileName, const FileExtensionsSet &FileExtensions)
Decides whether a file has a header file extension.
Definition: FileExtensionsUtils.cpp:57
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:23
clang::tidy::utils::parseFileExtensions
bool parseFileExtensions(StringRef AllFileExtensions, FileExtensionsSet &FileExtensions, StringRef Delimiters)
Parses header file extensions from a semicolon-separated list.
Definition: FileExtensionsUtils.cpp:35
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:67
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:29
Suffixes
static constexpr llvm::StringLiteral Suffixes
Definition: UppercaseLiteralSuffixCheck.cpp:33
FileName
StringRef FileName
Definition: KernelNameRestrictionCheck.cpp:46
FileExtensionsUtils.h
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:17
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::utils::FileExtensionsSet
llvm::SmallSet< llvm::StringRef, 5 > FileExtensionsSet
Definition: FileExtensionsUtils.h:22