clang-tools  8.0.0svn
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 
11 #include "clang/Basic/CharInfo.h"
12 #include "llvm/Support/Path.h"
13 
14 namespace clang {
15 namespace tidy {
16 namespace utils {
17 
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 
27  SourceLocation Loc, SourceManager &SM,
28  const HeaderFileExtensionsSet &HeaderFileExtensions) {
29  PresumedLoc PresumedLocation = SM.getPresumedLoc(Loc);
30  return isHeaderFileExtension(PresumedLocation.getFilename(),
31  HeaderFileExtensions);
32 }
33 
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 
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
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.