clang-tools 6.0.0

HeaderFileExtensionsUtils.cpp
Go to the documentation of this file.
1 //===--- HeaderFileExtensionsUtils.cpp - clang-tidy--------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "HeaderFileExtensionsUtils.h"
11 #include "clang/Basic/CharInfo.h"
12 #include "llvm/Support/Path.h"
13
14 namespace clang {
15 namespace tidy {
16 namespace utils {
17
18 bool isExpansionLocInHeaderFile(
19 SourceLocation Loc, const SourceManager &SM,
20 const HeaderFileExtensionsSet &HeaderFileExtensions) {
21 SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
22 return isHeaderFileExtension(SM.getFilename(ExpansionLoc),
23 HeaderFileExtensions);
24 }
25
26 bool isPresumedLocInHeaderFile(
27 SourceLocation Loc, SourceManager &SM,
28 const HeaderFileExtensionsSet &HeaderFileExtensions) {
29 PresumedLoc PresumedLocation = SM.getPresumedLoc(Loc);
30 return isHeaderFileExtension(PresumedLocation.getFilename(),
31 HeaderFileExtensions);
32 }
33
34 bool isSpellingLocInHeaderFile(
35 SourceLocation Loc, SourceManager &SM,
36 const HeaderFileExtensionsSet &HeaderFileExtensions) {
37 SourceLocation SpellingLoc = SM.getSpellingLoc(Loc);
38 return isHeaderFileExtension(SM.getFilename(SpellingLoc),
39 HeaderFileExtensions);
40 }
41
42 bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions,
43 HeaderFileExtensionsSet &HeaderFileExtensions,
44 char delimiter) {
45 SmallVector<StringRef, 5> Suffixes;
46 AllHeaderFileExtensions.split(Suffixes, delimiter);
47 HeaderFileExtensions.clear();
48 for (StringRef Suffix : Suffixes) {
49 StringRef Extension = Suffix.trim();
50 for (StringRef::const_iterator it = Extension.begin();
51 it != Extension.end(); ++it) {
52 if (!isAlphanumeric(*it))
53 return false;
54 }
55 HeaderFileExtensions.insert(Extension);
56 }
57 return true;
58 }
59
60 bool isHeaderFileExtension(
61 StringRef FileName, const HeaderFileExtensionsSet &HeaderFileExtensions) {
62 StringRef extension = llvm::sys::path::extension(FileName);
63 if (extension.empty())
64 return false;
65 // Skip "." prefix.
66 return HeaderFileExtensions.count(extension.substr(1)) > 0;
67 }
68
69 } // namespace utils
70 } // namespace tidy
71 } // namespace clang
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:36
clang::tidy::utils::parseHeaderFileExtensions
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
Definition: HeaderFileExtensionsUtils.cpp:42
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:34
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:26
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:18
clang::tidy::utils::HeaderFileExtensionsSet
llvm::SmallSet< llvm::StringRef, 5 > HeaderFileExtensionsSet
Definition: HeaderFileExtensionsUtils.h:22
clang
Definition: AndroidTidyModule.cpp:28
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:60