diff options
author | Haojian Wu <hokein@google.com> | 2016-02-05 11:23:59 +0000 |
---|---|---|
committer | Haojian Wu <hokein@google.com> | 2016-02-05 11:23:59 +0000 |
commit | c2d7577cc552b248857253f99a401800bc010100 (patch) | |
tree | 1146fd04b4d6781a76196f3e3aacad3006722396 /clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.cpp | |
parent | 5990cd5a0c8fde4baceea0734ae9e086e987f577 (diff) | |
download | bcm5719-llvm-c2d7577cc552b248857253f99a401800bc010100.tar.gz bcm5719-llvm-c2d7577cc552b248857253f99a401800bc010100.zip |
[clang-tdiy] Add header file extension configuration support.
Summary: * Add a `HeaderFileExtensions` check option in misc-definitions-in-headers, google-build-namespaces and google-global-names-in-headers.
Reviewers: aaron.ballman, alexfh
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D16113
llvm-svn: 259879
Diffstat (limited to 'clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.cpp')
-rw-r--r-- | clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.cpp b/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.cpp new file mode 100644 index 00000000000..749ce6c6673 --- /dev/null +++ b/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.cpp @@ -0,0 +1,65 @@ +//===--- HeaderFileExtensionsUtils.cpp - clang-tidy--------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "HeaderFileExtensionsUtils.h" +#include "clang/Basic/CharInfo.h" + +namespace clang { +namespace tidy { +namespace utils { + +bool isExpansionLocInHeaderFile( + SourceLocation Loc, const SourceManager &SM, + const HeaderFileExtensionsSet &HeaderFileExtensions) { + SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc); + StringRef FileExtension = + llvm::sys::path::extension(SM.getFilename(ExpansionLoc)); + return HeaderFileExtensions.count(FileExtension.substr(1)) > 0; +} + +bool isPresumedLocInHeaderFile( + SourceLocation Loc, SourceManager &SM, + const HeaderFileExtensionsSet &HeaderFileExtensions) { + PresumedLoc PresumedLocation = SM.getPresumedLoc(Loc); + StringRef FileExtension = + llvm::sys::path::extension(PresumedLocation.getFilename()); + return HeaderFileExtensions.count(FileExtension.substr(1)) > 0; +} + +bool isSpellingLocInHeaderFile( + SourceLocation Loc, SourceManager &SM, + const HeaderFileExtensionsSet &HeaderFileExtensions) { + SourceLocation SpellingLoc = SM.getSpellingLoc(Loc); + StringRef FileExtension = + llvm::sys::path::extension(SM.getFilename(SpellingLoc)); + + return HeaderFileExtensions.count(FileExtension.substr(1)) > 0; +} + +bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, + HeaderFileExtensionsSet &HeaderFileExtensions, + char delimiter) { + SmallVector<StringRef, 5> Suffixes; + AllHeaderFileExtensions.split(Suffixes, delimiter); + HeaderFileExtensions.clear(); + for (StringRef Suffix : Suffixes) { + StringRef Extension = Suffix.trim(); + for (StringRef::const_iterator it = Extension.begin(); + it != Extension.end(); ++it) { + if (!isAlphanumeric(*it)) + return false; + } + HeaderFileExtensions.insert(Extension); + } + return true; +} + +} // namespace utils +} // namespace tidy +} // namespace clang |