2014-07-16 22:16:56 +08:00
|
|
|
//===--- UnnamedNamespaceInHeaderCheck.cpp - clang-tidy ---------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-07-16 22:16:56 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "UnnamedNamespaceInHeaderCheck.h"
|
2015-01-14 19:24:38 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2014-07-16 22:16:56 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2015-03-05 21:46:14 +08:00
|
|
|
namespace google {
|
2014-07-16 22:16:56 +08:00
|
|
|
namespace build {
|
|
|
|
|
2016-02-05 19:23:59 +08:00
|
|
|
UnnamedNamespaceInHeaderCheck::UnnamedNamespaceInHeaderCheck(
|
|
|
|
StringRef Name, ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
2017-07-20 20:02:03 +08:00
|
|
|
RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
|
|
|
|
"HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
|
2020-02-26 01:41:32 +08:00
|
|
|
if (!utils::parseFileExtensions(RawStringHeaderFileExtensions,
|
2020-03-05 00:55:03 +08:00
|
|
|
HeaderFileExtensions,
|
|
|
|
utils::defaultFileExtensionDelimiters())) {
|
2020-12-09 04:28:42 +08:00
|
|
|
this->configurationDiag("Invalid header file extension: '%0'")
|
|
|
|
<< RawStringHeaderFileExtensions;
|
2016-02-05 19:23:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnnamedNamespaceInHeaderCheck::storeOptions(
|
|
|
|
ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
|
|
|
|
}
|
|
|
|
|
2014-07-16 22:16:56 +08:00
|
|
|
void UnnamedNamespaceInHeaderCheck::registerMatchers(
|
|
|
|
ast_matchers::MatchFinder *Finder) {
|
2015-09-03 00:20:42 +08:00
|
|
|
Finder->addMatcher(namespaceDecl(isAnonymous()).bind("anonymousNamespace"),
|
|
|
|
this);
|
2014-07-16 22:16:56 +08:00
|
|
|
}
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
void UnnamedNamespaceInHeaderCheck::check(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
2014-07-16 22:16:56 +08:00
|
|
|
const auto *N = Result.Nodes.getNodeAs<NamespaceDecl>("anonymousNamespace");
|
2018-08-10 06:42:26 +08:00
|
|
|
SourceLocation Loc = N->getBeginLoc();
|
2014-07-16 22:16:56 +08:00
|
|
|
if (!Loc.isValid())
|
|
|
|
return;
|
|
|
|
|
2016-02-05 19:23:59 +08:00
|
|
|
if (utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager,
|
|
|
|
HeaderFileExtensions))
|
2016-01-08 23:21:40 +08:00
|
|
|
diag(Loc, "do not use unnamed namespaces in header files");
|
2014-07-16 22:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace build
|
2015-03-05 21:46:14 +08:00
|
|
|
} // namespace google
|
2014-07-16 22:16:56 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|