clang-tools  10.0.0svn
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 
10 #include "clang/Basic/CharInfo.h"
11 #include "llvm/Support/Path.h"
12 
13 namespace clang {
14 namespace tidy {
15 namespace utils {
16 
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 
26  SourceLocation Loc, SourceManager &SM,
27  const HeaderFileExtensionsSet &HeaderFileExtensions) {
28  PresumedLoc PresumedLocation = SM.getPresumedLoc(Loc);
29  return isHeaderFileExtension(PresumedLocation.getFilename(),
30  HeaderFileExtensions);
31 }
32 
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 
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
SourceLocation Loc
&#39;#&#39; location in the include directive
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const HeaderFileExtensionsSet &HeaderFileExtensions)
Checks whether spelling location of Loc is in header file.
bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const HeaderFileExtensionsSet &HeaderFileExtensions)
Checks whether presumed location of Loc is in header file.
bool isExpansionLocInHeaderFile(SourceLocation Loc, const SourceManager &SM, const HeaderFileExtensionsSet &HeaderFileExtensions)
Checks whether expansion location of Loc is in header file.
llvm::SmallSet< llvm::StringRef, 5 > HeaderFileExtensionsSet
PathRef FileName
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
bool isHeaderFileExtension(StringRef FileName, const HeaderFileExtensionsSet &HeaderFileExtensions)
Decides whether a file has a header file extension.
static constexpr llvm::StringLiteral Suffixes