clang-tools 10.0.0

HeaderFileExtensionsUtils.cpp
Go to the documentation of this file.
1 //===--- HeaderFileExtensionsUtils.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 "HeaderFileExtensionsUtils.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(
18 SourceLocation Loc, const SourceManager &SM,
19 const HeaderFileExtensionsSet &HeaderFileExtensions) {
20 SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
21 return isHeaderFileExtension(SM.getFilename(ExpansionLoc),
22 HeaderFileExtensions);
23 }
24
25 bool isPresumedLocInHeaderFile(
26 SourceLocation Loc, SourceManager &SM,
27 const HeaderFileExtensionsSet &HeaderFileExtensions) {
28 PresumedLoc PresumedLocation = SM.getPresumedLoc(Loc);
29 return isHeaderFileExtension(PresumedLocation.getFilename(),
30 HeaderFileExtensions);
31 }
32
33 bool isSpellingLocInHeaderFile(
34 SourceLocation Loc, SourceManager &SM,
35 const HeaderFileExtensionsSet &HeaderFileExtensions) {
36 SourceLocation SpellingLoc = SM.getSpellingLoc(Loc);
37 return isHeaderFileExtension(SM.getFilename(SpellingLoc),
38 HeaderFileExtensions);
39 }
40
41 bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions,
42 HeaderFileExtensionsSet &HeaderFileExtensions,
43 char delimiter) {
44 SmallVector<StringRef, 5> Suffixes;
45 AllHeaderFileExtensions.split(Suffixes, delimiter);
46 HeaderFileExtensions.clear();
47 for (StringRef Suffix : Suffixes) {
48 StringRef Extension = Suffix.trim();
49 for (StringRef::const_iterator it = Extension.begin();
50 it != Extension.end(); ++it) {
51 if (!isAlphanumeric(*it))
52 return false;
53 }
54 HeaderFileExtensions.insert(Extension);
55 }
56 return true;
57 }
58
59 bool isHeaderFileExtension(
60 StringRef FileName, const HeaderFileExtensionsSet &HeaderFileExtensions) {
61 StringRef extension = llvm::sys::path::extension(FileName);
62 if (extension.empty())
63 return false;
64 // Skip "." prefix.
65 return HeaderFileExtensions.count(extension.substr(1)) > 0;
66 }
67
68 } // namespace utils
69 } // namespace tidy
70 } // namespace clang
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::tidy::utils::parseHeaderFileExtensions
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
Definition: HeaderFileExtensionsUtils.cpp:41
clang::tidy::utils::isSpellingLocInHeaderFile
bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const HeaderFileExtensionsSet &HeaderFileExtensions)
Checks whether spelling location of Loc is in header file.
Definition: HeaderFileExtensionsUtils.cpp:33
clang::tidy::utils::isPresumedLocInHeaderFile
bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const HeaderFileExtensionsSet &HeaderFileExtensions)
Checks whether presumed location of Loc is in header file.
Definition: HeaderFileExtensionsUtils.cpp:25
clang::tidy::utils::isExpansionLocInHeaderFile
bool isExpansionLocInHeaderFile(SourceLocation Loc, const SourceManager &SM, const HeaderFileExtensionsSet &HeaderFileExtensions)
Checks whether expansion location of Loc is in header file.
Definition: HeaderFileExtensionsUtils.cpp:17
clang::tidy::utils::HeaderFileExtensionsSet
llvm::SmallSet< llvm::StringRef, 5 > HeaderFileExtensionsSet
Definition: HeaderFileExtensionsUtils.h:21
FileName
PathRef FileName
Definition: CodeComplete.cpp:1018
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
HeaderFileExtensionsUtils.h
clang::tidy::utils::isHeaderFileExtension
bool isHeaderFileExtension(StringRef FileName, const HeaderFileExtensionsSet &HeaderFileExtensions)
Decides whether a file has a header file extension.
Definition: HeaderFileExtensionsUtils.cpp:59
Suffixes
static constexpr llvm::StringLiteral Suffixes
Definition: UppercaseLiteralSuffixCheck.cpp:32