2019-10-03 01:18:57 +08:00
|
|
|
//===--- InitVariablesCheck.cpp - clang-tidy ------------------------------===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InitVariablesCheck.h"
|
|
|
|
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
2020-06-29 23:05:51 +08:00
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2019-10-03 01:18:57 +08:00
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace cppcoreguidelines {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
InitVariablesCheck::InitVariablesCheck(StringRef Name,
|
|
|
|
ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
2020-07-27 19:48:53 +08:00
|
|
|
IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
|
|
|
|
utils::IncludeSorter::IS_LLVM)),
|
2020-09-28 20:58:27 +08:00
|
|
|
MathHeader(Options.get("MathHeader", "<math.h>")) {}
|
2019-10-03 01:18:57 +08:00
|
|
|
|
2020-06-22 02:01:09 +08:00
|
|
|
void InitVariablesCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
2020-07-27 19:48:53 +08:00
|
|
|
Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle());
|
2020-06-22 02:01:09 +08:00
|
|
|
Options.store(Opts, "MathHeader", MathHeader);
|
|
|
|
}
|
|
|
|
|
2019-10-03 01:18:57 +08:00
|
|
|
void InitVariablesCheck::registerMatchers(MatchFinder *Finder) {
|
[clang-tidy] Fix false positive for cppcoreguidelines-init-variables
Summary: Fixes [[ https://bugs.llvm.org/show_bug.cgi?id=44746 | False positive for cppcoreguidelines-init-variables in range based for loop in template function ]]
Reviewers: aaron.ballman, alexfh, hokein, JonasToth, gribozavr2
Reviewed By: aaron.ballman
Subscribers: merge_guards_bot, xazax.hun, wuzish, nemanjai, kbarton, cfe-commits
Tags: #clang, #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D73843
2020-02-03 05:26:04 +08:00
|
|
|
std::string BadDecl = "badDecl";
|
|
|
|
Finder->addMatcher(
|
|
|
|
varDecl(unless(hasInitializer(anything())), unless(isInstantiated()),
|
|
|
|
isLocalVarDecl(), unless(isStaticLocal()), isDefinition(),
|
2020-07-01 20:40:18 +08:00
|
|
|
unless(hasParent(cxxCatchStmt())),
|
[clang-tidy] Fix false positive for cppcoreguidelines-init-variables
Summary: Fixes [[ https://bugs.llvm.org/show_bug.cgi?id=44746 | False positive for cppcoreguidelines-init-variables in range based for loop in template function ]]
Reviewers: aaron.ballman, alexfh, hokein, JonasToth, gribozavr2
Reviewed By: aaron.ballman
Subscribers: merge_guards_bot, xazax.hun, wuzish, nemanjai, kbarton, cfe-commits
Tags: #clang, #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D73843
2020-02-03 05:26:04 +08:00
|
|
|
optionally(hasParent(declStmt(hasParent(
|
|
|
|
cxxForRangeStmt(hasLoopVariable(varDecl().bind(BadDecl))))))),
|
|
|
|
unless(equalsBoundNode(BadDecl)))
|
|
|
|
.bind("vardecl"),
|
|
|
|
this);
|
2019-10-03 01:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void InitVariablesCheck::registerPPCallbacks(const SourceManager &SM,
|
|
|
|
Preprocessor *PP,
|
|
|
|
Preprocessor *ModuleExpanderPP) {
|
2020-07-27 19:48:53 +08:00
|
|
|
IncludeInserter.registerPreprocessor(PP);
|
2019-10-03 01:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>("vardecl");
|
|
|
|
const ASTContext &Context = *Result.Context;
|
|
|
|
const SourceManager &Source = Context.getSourceManager();
|
|
|
|
|
|
|
|
// We want to warn about cases where the type name
|
|
|
|
// comes from a macro like this:
|
|
|
|
//
|
|
|
|
// TYPENAME_FROM_MACRO var;
|
|
|
|
//
|
|
|
|
// but not if the entire declaration comes from
|
|
|
|
// one:
|
|
|
|
//
|
|
|
|
// DEFINE_SOME_VARIABLE();
|
|
|
|
//
|
|
|
|
// or if the definition comes from a macro like SWAP
|
|
|
|
// that uses an internal temporary variable.
|
|
|
|
//
|
|
|
|
// Thus check that the variable name does
|
|
|
|
// not come from a macro expansion.
|
|
|
|
if (MatchedDecl->getEndLoc().isMacroID())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QualType TypePtr = MatchedDecl->getType();
|
|
|
|
const char *InitializationString = nullptr;
|
|
|
|
bool AddMathInclude = false;
|
|
|
|
|
|
|
|
if (TypePtr->isIntegerType())
|
|
|
|
InitializationString = " = 0";
|
|
|
|
else if (TypePtr->isFloatingType()) {
|
|
|
|
InitializationString = " = NAN";
|
|
|
|
AddMathInclude = true;
|
|
|
|
} else if (TypePtr->isAnyPointerType()) {
|
|
|
|
if (getLangOpts().CPlusPlus11)
|
|
|
|
InitializationString = " = nullptr";
|
|
|
|
else
|
|
|
|
InitializationString = " = NULL";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (InitializationString) {
|
|
|
|
auto Diagnostic =
|
|
|
|
diag(MatchedDecl->getLocation(), "variable %0 is not initialized")
|
|
|
|
<< MatchedDecl
|
|
|
|
<< FixItHint::CreateInsertion(
|
|
|
|
MatchedDecl->getLocation().getLocWithOffset(
|
|
|
|
MatchedDecl->getName().size()),
|
|
|
|
InitializationString);
|
|
|
|
if (AddMathInclude) {
|
2020-07-27 19:48:53 +08:00
|
|
|
Diagnostic << IncludeInserter.createIncludeInsertion(
|
2020-09-28 20:58:27 +08:00
|
|
|
Source.getFileID(MatchedDecl->getBeginLoc()), MathHeader);
|
2019-10-03 01:18:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace cppcoreguidelines
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|