Bug 1410472 - clang-plugin follows the LLVM coding style for real r=mystor
MozReview-Commit-ID: AXrQEjWzxvg
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
#include "ArithmeticArgChecker.h"
|
#include "ArithmeticArgChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void ArithmeticArgChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void ArithmeticArgChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(
|
AstMatcher->addMatcher(
|
||||||
callExpr(allOf(hasDeclaration(noArithmeticExprInArgs()),
|
callExpr(allOf(hasDeclaration(noArithmeticExprInArgs()),
|
||||||
anyOf(hasDescendant(
|
anyOf(hasDescendant(
|
||||||
@@ -45,14 +45,16 @@ void ArithmeticArgChecker::registerMatchers(MatchFinder* AstMatcher) {
|
|||||||
this);
|
this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArithmeticArgChecker::check(
|
void ArithmeticArgChecker::check(const MatchFinder::MatchResult &Result) {
|
||||||
const MatchFinder::MatchResult &Result) {
|
const char *Error =
|
||||||
const char* Error = "cannot pass an arithmetic expression of built-in types to %0";
|
"cannot pass an arithmetic expression of built-in types to %0";
|
||||||
const Expr *Expression = Result.Nodes.getNodeAs<Expr>("node");
|
const Expr *Expression = Result.Nodes.getNodeAs<Expr>("node");
|
||||||
if (const CallExpr *Call = Result.Nodes.getNodeAs<CallExpr>("call")) {
|
if (const CallExpr *Call = Result.Nodes.getNodeAs<CallExpr>("call")) {
|
||||||
diag(Expression->getLocStart(), Error, DiagnosticIDs::Error) << Call->getDirectCallee();
|
diag(Expression->getLocStart(), Error, DiagnosticIDs::Error)
|
||||||
|
<< Call->getDirectCallee();
|
||||||
} else if (const CXXConstructExpr *Ctr =
|
} else if (const CXXConstructExpr *Ctr =
|
||||||
Result.Nodes.getNodeAs<CXXConstructExpr>("call")) {
|
Result.Nodes.getNodeAs<CXXConstructExpr>("call")) {
|
||||||
diag(Expression->getLocStart(), Error, DiagnosticIDs::Error) << Ctr->getConstructor();
|
diag(Expression->getLocStart(), Error, DiagnosticIDs::Error)
|
||||||
|
<< Ctr->getConstructor();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,9 @@
|
|||||||
|
|
||||||
class ArithmeticArgChecker : public BaseCheck {
|
class ArithmeticArgChecker : public BaseCheck {
|
||||||
public:
|
public:
|
||||||
ArithmeticArgChecker(StringRef CheckName,
|
ArithmeticArgChecker(StringRef CheckName, ContextType *Context = nullptr)
|
||||||
ContextType *Context = nullptr)
|
: BaseCheck(CheckName, Context) {}
|
||||||
: BaseCheck(CheckName, Context) {}
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,19 +5,16 @@
|
|||||||
#include "AssertAssignmentChecker.h"
|
#include "AssertAssignmentChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void AssertAssignmentChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void AssertAssignmentChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(
|
AstMatcher->addMatcher(
|
||||||
callExpr(isAssertAssignmentTestFunc()).bind("funcCall"),
|
callExpr(isAssertAssignmentTestFunc()).bind("funcCall"), this);
|
||||||
this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssertAssignmentChecker::check(
|
void AssertAssignmentChecker::check(const MatchFinder::MatchResult &Result) {
|
||||||
const MatchFinder::MatchResult &Result) {
|
|
||||||
const CallExpr *FuncCall = Result.Nodes.getNodeAs<CallExpr>("funcCall");
|
const CallExpr *FuncCall = Result.Nodes.getNodeAs<CallExpr>("funcCall");
|
||||||
|
|
||||||
if (FuncCall && hasSideEffectAssignment(FuncCall)) {
|
if (FuncCall && hasSideEffectAssignment(FuncCall)) {
|
||||||
diag(FuncCall->getLocStart(),
|
diag(FuncCall->getLocStart(), "Forbidden assignment in assert expression",
|
||||||
"Forbidden assignment in assert expression",
|
|
||||||
DiagnosticIDs::Error);
|
DiagnosticIDs::Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,9 @@
|
|||||||
|
|
||||||
class AssertAssignmentChecker : public BaseCheck {
|
class AssertAssignmentChecker : public BaseCheck {
|
||||||
public:
|
public:
|
||||||
AssertAssignmentChecker(StringRef CheckName,
|
AssertAssignmentChecker(StringRef CheckName, ContextType *Context = nullptr)
|
||||||
ContextType *Context = nullptr)
|
: BaseCheck(CheckName, Context) {}
|
||||||
: BaseCheck(CheckName, Context) {}
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ typedef MozContext ContextType;
|
|||||||
|
|
||||||
class BaseCheck : public MatchFinder::MatchCallback {
|
class BaseCheck : public MatchFinder::MatchCallback {
|
||||||
public:
|
public:
|
||||||
BaseCheck(StringRef CheckName, ContextType* Context) {}
|
BaseCheck(StringRef CheckName, ContextType *Context) {}
|
||||||
virtual void registerMatchers(MatchFinder *Finder) {}
|
virtual void registerMatchers(MatchFinder *Finder) {}
|
||||||
virtual void registerPPCallbacks(CompilerInstance& CI) {}
|
virtual void registerPPCallbacks(CompilerInstance &CI) {}
|
||||||
virtual void check(const MatchFinder::MatchResult &Result) {}
|
virtual void check(const MatchFinder::MatchResult &Result) {}
|
||||||
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description,
|
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description,
|
||||||
DiagnosticIDs::Level Level = DiagnosticIDs::Warning) {
|
DiagnosticIDs::Level Level = DiagnosticIDs::Warning) {
|
||||||
@@ -28,7 +28,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ASTContext* Context;
|
ASTContext *Context;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include "CanRunScriptChecker.h"
|
#include "CanRunScriptChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void CanRunScriptChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void CanRunScriptChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
auto InvalidArg =
|
auto InvalidArg =
|
||||||
// We want to find any expression,
|
// We want to find any expression,
|
||||||
ignoreTrivials(expr(
|
ignoreTrivials(expr(
|
||||||
@@ -19,17 +19,15 @@ void CanRunScriptChecker::registerMatchers(MatchFinder* AstMatcher) {
|
|||||||
// and which is not a parameter of the parent function,
|
// and which is not a parameter of the parent function,
|
||||||
unless(declRefExpr(to(parmVarDecl()))),
|
unless(declRefExpr(to(parmVarDecl()))),
|
||||||
// and which is not a MOZ_KnownLive wrapped value.
|
// and which is not a MOZ_KnownLive wrapped value.
|
||||||
unless(callExpr(callee(
|
unless(callExpr(callee(functionDecl(hasName("MOZ_KnownLive"))))),
|
||||||
functionDecl(hasName("MOZ_KnownLive"))))),
|
|
||||||
expr().bind("invalidArg")));
|
expr().bind("invalidArg")));
|
||||||
|
|
||||||
auto OptionalInvalidExplicitArg =
|
auto OptionalInvalidExplicitArg = anyOf(
|
||||||
anyOf(
|
// We want to find any argument which is invalid.
|
||||||
// We want to find any argument which is invalid.
|
hasAnyArgument(InvalidArg),
|
||||||
hasAnyArgument(InvalidArg),
|
|
||||||
|
|
||||||
// This makes this matcher optional.
|
// This makes this matcher optional.
|
||||||
anything());
|
anything());
|
||||||
|
|
||||||
// Please not that the hasCanRunScriptAnnotation() matchers are not present
|
// Please not that the hasCanRunScriptAnnotation() matchers are not present
|
||||||
// directly in the cxxMemberCallExpr, callExpr and constructExpr matchers
|
// directly in the cxxMemberCallExpr, callExpr and constructExpr matchers
|
||||||
@@ -46,9 +44,7 @@ void CanRunScriptChecker::registerMatchers(MatchFinder* AstMatcher) {
|
|||||||
anyOf(
|
anyOf(
|
||||||
// which derefs into an invalid arg,
|
// which derefs into an invalid arg,
|
||||||
on(cxxOperatorCallExpr(
|
on(cxxOperatorCallExpr(
|
||||||
anyOf(
|
anyOf(hasAnyArgument(InvalidArg), anything()))),
|
||||||
hasAnyArgument(InvalidArg),
|
|
||||||
anything()))),
|
|
||||||
// or is an invalid arg.
|
// or is an invalid arg.
|
||||||
on(InvalidArg),
|
on(InvalidArg),
|
||||||
|
|
||||||
@@ -57,13 +53,11 @@ void CanRunScriptChecker::registerMatchers(MatchFinder* AstMatcher) {
|
|||||||
// or a regular call expression,
|
// or a regular call expression,
|
||||||
callExpr(
|
callExpr(
|
||||||
// which optionally has an invalid arg.
|
// which optionally has an invalid arg.
|
||||||
OptionalInvalidExplicitArg,
|
OptionalInvalidExplicitArg, expr().bind("callExpr")),
|
||||||
expr().bind("callExpr")),
|
|
||||||
// or a construct expression,
|
// or a construct expression,
|
||||||
cxxConstructExpr(
|
cxxConstructExpr(
|
||||||
// which optionally has an invalid arg.
|
// which optionally has an invalid arg.
|
||||||
OptionalInvalidExplicitArg,
|
OptionalInvalidExplicitArg, expr().bind("constructExpr"))),
|
||||||
expr().bind("constructExpr"))),
|
|
||||||
|
|
||||||
anyOf(
|
anyOf(
|
||||||
// We want to match the parent function.
|
// We want to match the parent function.
|
||||||
@@ -80,52 +74,52 @@ void CanRunScriptChecker::onStartOfTranslationUnit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
/// This class is a callback used internally to match function declarations
|
/// This class is a callback used internally to match function declarations
|
||||||
/// with the MOZ_CAN_RUN_SCRIPT annotation, adding these functions and all
|
/// with the MOZ_CAN_RUN_SCRIPT annotation, adding these functions and all
|
||||||
/// the methods they override to the can-run-script function set.
|
/// the methods they override to the can-run-script function set.
|
||||||
class FuncSetCallback : public MatchFinder::MatchCallback {
|
class FuncSetCallback : public MatchFinder::MatchCallback {
|
||||||
public:
|
public:
|
||||||
FuncSetCallback(std::unordered_set<const FunctionDecl*> &FuncSet)
|
FuncSetCallback(std::unordered_set<const FunctionDecl *> &FuncSet)
|
||||||
: CanRunScriptFuncs(FuncSet) {}
|
: CanRunScriptFuncs(FuncSet) {}
|
||||||
|
|
||||||
void run(const MatchFinder::MatchResult &Result) override;
|
void run(const MatchFinder::MatchResult &Result) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// This method recursively adds all the methods overriden by the given
|
/// This method recursively adds all the methods overriden by the given
|
||||||
/// paremeter.
|
/// paremeter.
|
||||||
void addAllOverriddenMethodsRecursively(const CXXMethodDecl* Method);
|
void addAllOverriddenMethodsRecursively(const CXXMethodDecl *Method);
|
||||||
|
|
||||||
std::unordered_set<const FunctionDecl*> &CanRunScriptFuncs;
|
std::unordered_set<const FunctionDecl *> &CanRunScriptFuncs;
|
||||||
};
|
};
|
||||||
|
|
||||||
void FuncSetCallback::run(const MatchFinder::MatchResult &Result) {
|
void FuncSetCallback::run(const MatchFinder::MatchResult &Result) {
|
||||||
const FunctionDecl* Func =
|
const FunctionDecl *Func =
|
||||||
Result.Nodes.getNodeAs<FunctionDecl>("canRunScriptFunction");
|
Result.Nodes.getNodeAs<FunctionDecl>("canRunScriptFunction");
|
||||||
|
|
||||||
CanRunScriptFuncs.insert(Func);
|
CanRunScriptFuncs.insert(Func);
|
||||||
|
|
||||||
// If this is a method, we check the methods it overrides.
|
// If this is a method, we check the methods it overrides.
|
||||||
if (auto* Method = dyn_cast<CXXMethodDecl>(Func)) {
|
if (auto *Method = dyn_cast<CXXMethodDecl>(Func)) {
|
||||||
addAllOverriddenMethodsRecursively(Method);
|
addAllOverriddenMethodsRecursively(Method);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void FuncSetCallback::addAllOverriddenMethodsRecursively(
|
void FuncSetCallback::addAllOverriddenMethodsRecursively(
|
||||||
const CXXMethodDecl* Method) {
|
const CXXMethodDecl *Method) {
|
||||||
for (auto OverriddenMethod : Method->overridden_methods()) {
|
for (auto OverriddenMethod : Method->overridden_methods()) {
|
||||||
CanRunScriptFuncs.insert(OverriddenMethod);
|
CanRunScriptFuncs.insert(OverriddenMethod);
|
||||||
|
|
||||||
// If this is not the definition, we also add the definition (if it
|
// If this is not the definition, we also add the definition (if it
|
||||||
// exists) to the set.
|
// exists) to the set.
|
||||||
if (!OverriddenMethod->isThisDeclarationADefinition()) {
|
if (!OverriddenMethod->isThisDeclarationADefinition()) {
|
||||||
if (auto Def = OverriddenMethod->getDefinition()) {
|
if (auto Def = OverriddenMethod->getDefinition()) {
|
||||||
CanRunScriptFuncs.insert(Def);
|
CanRunScriptFuncs.insert(Def);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addAllOverriddenMethodsRecursively(OverriddenMethod);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addAllOverriddenMethodsRecursively(OverriddenMethod);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void CanRunScriptChecker::buildFuncSet(ASTContext *Context) {
|
void CanRunScriptChecker::buildFuncSet(ASTContext *Context) {
|
||||||
@@ -135,16 +129,15 @@ void CanRunScriptChecker::buildFuncSet(ASTContext *Context) {
|
|||||||
// a MOZ_CAN_RUN_SCRIPT annotation.
|
// a MOZ_CAN_RUN_SCRIPT annotation.
|
||||||
FuncSetCallback Callback(CanRunScriptFuncs);
|
FuncSetCallback Callback(CanRunScriptFuncs);
|
||||||
// We add the matcher to the finder, linking it to our callback.
|
// We add the matcher to the finder, linking it to our callback.
|
||||||
Finder.addMatcher(functionDecl(hasCanRunScriptAnnotation())
|
Finder.addMatcher(
|
||||||
.bind("canRunScriptFunction"),
|
functionDecl(hasCanRunScriptAnnotation()).bind("canRunScriptFunction"),
|
||||||
&Callback);
|
&Callback);
|
||||||
|
|
||||||
// We start the analysis, given the ASTContext our main checker is in.
|
// We start the analysis, given the ASTContext our main checker is in.
|
||||||
Finder.matchAST(*Context);
|
Finder.matchAST(*Context);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CanRunScriptChecker::check(
|
void CanRunScriptChecker::check(const MatchFinder::MatchResult &Result) {
|
||||||
const MatchFinder::MatchResult &Result) {
|
|
||||||
|
|
||||||
// If the set of functions which can run script is not yet built, then build
|
// If the set of functions which can run script is not yet built, then build
|
||||||
// it.
|
// it.
|
||||||
@@ -153,39 +146,38 @@ void CanRunScriptChecker::check(
|
|||||||
IsFuncSetBuilt = true;
|
IsFuncSetBuilt = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* ErrorInvalidArg =
|
const char *ErrorInvalidArg =
|
||||||
"arguments must all be strong refs or parent parameters when calling a "
|
"arguments must all be strong refs or parent parameters when calling a "
|
||||||
"function marked as MOZ_CAN_RUN_SCRIPT (including the implicit object "
|
"function marked as MOZ_CAN_RUN_SCRIPT (including the implicit object "
|
||||||
"argument)";
|
"argument)";
|
||||||
|
|
||||||
const char* ErrorNonCanRunScriptParent =
|
const char *ErrorNonCanRunScriptParent =
|
||||||
"functions marked as MOZ_CAN_RUN_SCRIPT can only be called from "
|
"functions marked as MOZ_CAN_RUN_SCRIPT can only be called from "
|
||||||
"functions also marked as MOZ_CAN_RUN_SCRIPT";
|
"functions also marked as MOZ_CAN_RUN_SCRIPT";
|
||||||
const char* NoteNonCanRunScriptParent =
|
const char *NoteNonCanRunScriptParent = "parent function declared here";
|
||||||
"parent function declared here";
|
|
||||||
|
|
||||||
const Expr* InvalidArg = Result.Nodes.getNodeAs<Expr>("invalidArg");
|
const Expr *InvalidArg = Result.Nodes.getNodeAs<Expr>("invalidArg");
|
||||||
|
|
||||||
const CallExpr* Call = Result.Nodes.getNodeAs<CallExpr>("callExpr");
|
const CallExpr *Call = Result.Nodes.getNodeAs<CallExpr>("callExpr");
|
||||||
// If we don't find the FunctionDecl linked to this call or if it's not marked
|
// If we don't find the FunctionDecl linked to this call or if it's not marked
|
||||||
// as can-run-script, consider that we didn't find a match.
|
// as can-run-script, consider that we didn't find a match.
|
||||||
if (Call && (!Call->getDirectCallee() ||
|
if (Call && (!Call->getDirectCallee() ||
|
||||||
!CanRunScriptFuncs.count(Call->getDirectCallee()))) {
|
!CanRunScriptFuncs.count(Call->getDirectCallee()))) {
|
||||||
Call = nullptr;
|
Call = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
const CXXConstructExpr* Construct =
|
const CXXConstructExpr *Construct =
|
||||||
Result.Nodes.getNodeAs<CXXConstructExpr>("constructExpr");
|
Result.Nodes.getNodeAs<CXXConstructExpr>("constructExpr");
|
||||||
|
|
||||||
// If we don't find the CXXConstructorDecl linked to this construct expression
|
// If we don't find the CXXConstructorDecl linked to this construct expression
|
||||||
// or if it's not marked as can-run-script, consider that we didn't find a
|
// or if it's not marked as can-run-script, consider that we didn't find a
|
||||||
// match.
|
// match.
|
||||||
if (Construct && (!Construct->getConstructor() ||
|
if (Construct && (!Construct->getConstructor() ||
|
||||||
!CanRunScriptFuncs.count(Construct->getConstructor()))) {
|
!CanRunScriptFuncs.count(Construct->getConstructor()))) {
|
||||||
Construct = nullptr;
|
Construct = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
const FunctionDecl* ParentFunction =
|
const FunctionDecl *ParentFunction =
|
||||||
Result.Nodes.getNodeAs<FunctionDecl>("nonCanRunScriptParentFunction");
|
Result.Nodes.getNodeAs<FunctionDecl>("nonCanRunScriptParentFunction");
|
||||||
// If the parent function can run script, consider that we didn't find a match
|
// If the parent function can run script, consider that we didn't find a match
|
||||||
// because we only care about parent functions which can't run script.
|
// because we only care about parent functions which can't run script.
|
||||||
@@ -193,7 +185,6 @@ void CanRunScriptChecker::check(
|
|||||||
ParentFunction = nullptr;
|
ParentFunction = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Get the call range from either the CallExpr or the ConstructExpr.
|
// Get the call range from either the CallExpr or the ConstructExpr.
|
||||||
SourceRange CallRange;
|
SourceRange CallRange;
|
||||||
if (Call) {
|
if (Call) {
|
||||||
|
|||||||
@@ -10,10 +10,9 @@
|
|||||||
|
|
||||||
class CanRunScriptChecker : public BaseCheck {
|
class CanRunScriptChecker : public BaseCheck {
|
||||||
public:
|
public:
|
||||||
CanRunScriptChecker(StringRef CheckName,
|
CanRunScriptChecker(StringRef CheckName, ContextType *Context = nullptr)
|
||||||
ContextType *Context = nullptr)
|
: BaseCheck(CheckName, Context) {}
|
||||||
: BaseCheck(CheckName, Context) {}
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
|
|
||||||
// Simply initialize the can-run-script function set at the beginning of each
|
// Simply initialize the can-run-script function set at the beginning of each
|
||||||
@@ -26,7 +25,7 @@ private:
|
|||||||
void buildFuncSet(ASTContext *Context);
|
void buildFuncSet(ASTContext *Context);
|
||||||
|
|
||||||
bool IsFuncSetBuilt;
|
bool IsFuncSetBuilt;
|
||||||
std::unordered_set<const FunctionDecl*> CanRunScriptFuncs;
|
std::unordered_set<const FunctionDecl *> CanRunScriptFuncs;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ AST_MATCHER(CXXMethodDecl, isRValueRefQualified) {
|
|||||||
return Node.getRefQualifier() == RQ_RValue;
|
return Node.getRefQualifier() == RQ_RValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
AST_POLYMORPHIC_MATCHER(isFirstParty, \
|
AST_POLYMORPHIC_MATCHER(isFirstParty,
|
||||||
AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt)) {
|
AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt)) {
|
||||||
return !inThirdPartyPath(&Node, &Finder->getASTContext()) &&
|
return !inThirdPartyPath(&Node, &Finder->getASTContext()) &&
|
||||||
!ASTIsInSystemHeader(Finder->getASTContext(), Node);
|
!ASTIsInSystemHeader(Finder->getASTContext(), Node);
|
||||||
@@ -304,20 +304,20 @@ AST_MATCHER_P(Stmt, forFunction, internal::Matcher<FunctionDecl>,
|
|||||||
|
|
||||||
llvm::SmallVector<ast_type_traits::DynTypedNode, 8> Stack(Parents.begin(),
|
llvm::SmallVector<ast_type_traits::DynTypedNode, 8> Stack(Parents.begin(),
|
||||||
Parents.end());
|
Parents.end());
|
||||||
while(!Stack.empty()) {
|
while (!Stack.empty()) {
|
||||||
const auto &CurNode = Stack.back();
|
const auto &CurNode = Stack.back();
|
||||||
Stack.pop_back();
|
Stack.pop_back();
|
||||||
if(const auto *FuncDeclNode = CurNode.get<FunctionDecl>()) {
|
if (const auto *FuncDeclNode = CurNode.get<FunctionDecl>()) {
|
||||||
if(InnerMatcher.matches(*FuncDeclNode, Finder, Builder)) {
|
if (InnerMatcher.matches(*FuncDeclNode, Finder, Builder)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else if(const auto *LambdaExprNode = CurNode.get<LambdaExpr>()) {
|
} else if (const auto *LambdaExprNode = CurNode.get<LambdaExpr>()) {
|
||||||
if(InnerMatcher.matches(*LambdaExprNode->getCallOperator(),
|
if (InnerMatcher.matches(*LambdaExprNode->getCallOperator(), Finder,
|
||||||
Finder, Builder)) {
|
Builder)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for(const auto &Parent: Finder->getASTContext().getParents(CurNode))
|
for (const auto &Parent : Finder->getASTContext().getParents(CurNode))
|
||||||
Stack.push_back(Parent);
|
Stack.push_back(Parent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -325,7 +325,7 @@ AST_MATCHER_P(Stmt, forFunction, internal::Matcher<FunctionDecl>,
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
} // namespace ast_matchers
|
||||||
}
|
} // namespace clang
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -11,24 +11,19 @@ CustomTypeAnnotation GlobalClass =
|
|||||||
CustomTypeAnnotation("moz_global_class", "global");
|
CustomTypeAnnotation("moz_global_class", "global");
|
||||||
CustomTypeAnnotation NonHeapClass =
|
CustomTypeAnnotation NonHeapClass =
|
||||||
CustomTypeAnnotation("moz_nonheap_class", "non-heap");
|
CustomTypeAnnotation("moz_nonheap_class", "non-heap");
|
||||||
CustomTypeAnnotation HeapClass =
|
CustomTypeAnnotation HeapClass = CustomTypeAnnotation("moz_heap_class", "heap");
|
||||||
CustomTypeAnnotation("moz_heap_class", "heap");
|
|
||||||
CustomTypeAnnotation NonTemporaryClass =
|
CustomTypeAnnotation NonTemporaryClass =
|
||||||
CustomTypeAnnotation("moz_non_temporary_class", "non-temporary");
|
CustomTypeAnnotation("moz_non_temporary_class", "non-temporary");
|
||||||
|
|
||||||
void CustomTypeAnnotation::dumpAnnotationReason(BaseCheck &Check,
|
void CustomTypeAnnotation::dumpAnnotationReason(BaseCheck &Check, QualType T,
|
||||||
QualType T,
|
|
||||||
SourceLocation Loc) {
|
SourceLocation Loc) {
|
||||||
const char* Inherits =
|
const char *Inherits =
|
||||||
"%1 is a %0 type because it inherits from a %0 type %2";
|
"%1 is a %0 type because it inherits from a %0 type %2";
|
||||||
const char* Member =
|
const char *Member = "%1 is a %0 type because member %2 is a %0 type %3";
|
||||||
"%1 is a %0 type because member %2 is a %0 type %3";
|
const char *Array = "%1 is a %0 type because it is an array of %0 type %2";
|
||||||
const char* Array =
|
const char *Templ =
|
||||||
"%1 is a %0 type because it is an array of %0 type %2";
|
|
||||||
const char* Templ =
|
|
||||||
"%1 is a %0 type because it has a template argument %0 type %2";
|
"%1 is a %0 type because it has a template argument %0 type %2";
|
||||||
const char* Implicit =
|
const char *Implicit = "%1 is a %0 type because %2";
|
||||||
"%1 is a %0 type because %2";
|
|
||||||
|
|
||||||
AnnotationReason Reason = directAnnotationReason(T);
|
AnnotationReason Reason = directAnnotationReason(T);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@@ -41,7 +36,7 @@ void CustomTypeAnnotation::dumpAnnotationReason(BaseCheck &Check,
|
|||||||
assert(Declaration && "This type should be a C++ class");
|
assert(Declaration && "This type should be a C++ class");
|
||||||
|
|
||||||
Check.diag(Declaration->getLocation(), Inherits, DiagnosticIDs::Note)
|
Check.diag(Declaration->getLocation(), Inherits, DiagnosticIDs::Note)
|
||||||
<< Pretty << T << Reason.Type;
|
<< Pretty << T << Reason.Type;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case RK_Field:
|
case RK_Field:
|
||||||
@@ -53,7 +48,7 @@ void CustomTypeAnnotation::dumpAnnotationReason(BaseCheck &Check,
|
|||||||
assert(Declaration && "This type should be a C++ class");
|
assert(Declaration && "This type should be a C++ class");
|
||||||
|
|
||||||
Check.diag(Declaration->getLocation(), Templ, DiagnosticIDs::Note)
|
Check.diag(Declaration->getLocation(), Templ, DiagnosticIDs::Note)
|
||||||
<< Pretty << T << Reason.Type;
|
<< Pretty << T << Reason.Type;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case RK_Implicit: {
|
case RK_Implicit: {
|
||||||
@@ -61,7 +56,7 @@ void CustomTypeAnnotation::dumpAnnotationReason(BaseCheck &Check,
|
|||||||
assert(Declaration && "This type should be a TagDecl");
|
assert(Declaration && "This type should be a TagDecl");
|
||||||
|
|
||||||
Check.diag(Declaration->getLocation(), Implicit, DiagnosticIDs::Note)
|
Check.diag(Declaration->getLocation(), Implicit, DiagnosticIDs::Note)
|
||||||
<< Pretty << T << Reason.ImplicitReason;
|
<< Pretty << T << Reason.ImplicitReason;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -41,12 +41,10 @@ public:
|
|||||||
bool hasEffectiveAnnotation(QualType T) {
|
bool hasEffectiveAnnotation(QualType T) {
|
||||||
return directAnnotationReason(T).valid();
|
return directAnnotationReason(T).valid();
|
||||||
}
|
}
|
||||||
void dumpAnnotationReason(BaseCheck &Check, QualType T,
|
void dumpAnnotationReason(BaseCheck &Check, QualType T, SourceLocation Loc);
|
||||||
SourceLocation Loc);
|
|
||||||
|
|
||||||
void reportErrorIfPresent(BaseCheck &Check, QualType T,
|
void reportErrorIfPresent(BaseCheck &Check, QualType T, SourceLocation Loc,
|
||||||
SourceLocation Loc, const char* Error,
|
const char *Error, const char *Note) {
|
||||||
const char* Note) {
|
|
||||||
if (hasEffectiveAnnotation(T)) {
|
if (hasEffectiveAnnotation(T)) {
|
||||||
Check.diag(Loc, Error, DiagnosticIDs::Error) << T;
|
Check.diag(Loc, Error, DiagnosticIDs::Error) << T;
|
||||||
Check.diag(Loc, Note, DiagnosticIDs::Note);
|
Check.diag(Loc, Note, DiagnosticIDs::Note);
|
||||||
|
|||||||
@@ -42,36 +42,35 @@ void DanglingOnTemporaryChecker::registerMatchers(MatchFinder *AstMatcher) {
|
|||||||
// Main checker //
|
// Main checker //
|
||||||
//////////////////
|
//////////////////
|
||||||
|
|
||||||
auto hasParentCall =
|
auto hasParentCall = hasParent(expr(
|
||||||
hasParent(expr(anyOf(
|
anyOf(cxxOperatorCallExpr(
|
||||||
cxxOperatorCallExpr(
|
// If we're in a lamda, we may have an operator call expression
|
||||||
// If we're in a lamda, we may have an operator call expression
|
// ancestor in the AST, but the temporary we're matching
|
||||||
// ancestor in the AST, but the temporary we're matching
|
// against is not going to have the same lifetime as the
|
||||||
// against is not going to have the same lifetime as the
|
// constructor call.
|
||||||
// constructor call.
|
unless(has(expr(ignoreTrivials(lambdaExpr())))),
|
||||||
unless(has(expr(ignoreTrivials(lambdaExpr())))),
|
expr().bind("parentOperatorCallExpr")),
|
||||||
expr().bind("parentOperatorCallExpr")),
|
callExpr(
|
||||||
callExpr(
|
// If we're in a lamda, we may have a call expression
|
||||||
// If we're in a lamda, we may have a call expression
|
// ancestor in the AST, but the temporary we're matching
|
||||||
// ancestor in the AST, but the temporary we're matching
|
// against is not going to have the same lifetime as the
|
||||||
// against is not going to have the same lifetime as the
|
// function call.
|
||||||
// function call.
|
unless(has(expr(ignoreTrivials(lambdaExpr())))),
|
||||||
unless(has(expr(ignoreTrivials(lambdaExpr())))),
|
expr().bind("parentCallExpr")),
|
||||||
expr().bind("parentCallExpr")),
|
objcMessageExpr(
|
||||||
objcMessageExpr(
|
// If we're in a lamda, we may have an objc message expression
|
||||||
// If we're in a lamda, we may have an objc message expression
|
// ancestor in the AST, but the temporary we're matching
|
||||||
// ancestor in the AST, but the temporary we're matching
|
// against is not going to have the same lifetime as the
|
||||||
// against is not going to have the same lifetime as the
|
// function call.
|
||||||
// function call.
|
unless(has(expr(ignoreTrivials(lambdaExpr())))),
|
||||||
unless(has(expr(ignoreTrivials(lambdaExpr())))),
|
expr().bind("parentObjCMessageExpr")),
|
||||||
expr().bind("parentObjCMessageExpr")),
|
cxxConstructExpr(
|
||||||
cxxConstructExpr(
|
// If we're in a lamda, we may have a construct expression
|
||||||
// If we're in a lamda, we may have a construct expression
|
// ancestor in the AST, but the temporary we're matching
|
||||||
// ancestor in the AST, but the temporary we're matching
|
// against is not going to have the same lifetime as the
|
||||||
// against is not going to have the same lifetime as the
|
// constructor call.
|
||||||
// constructor call.
|
unless(has(expr(ignoreTrivials(lambdaExpr())))),
|
||||||
unless(has(expr(ignoreTrivials(lambdaExpr())))),
|
expr().bind("parentConstructExpr")))));
|
||||||
expr().bind("parentConstructExpr")))));
|
|
||||||
|
|
||||||
AstMatcher->addMatcher(
|
AstMatcher->addMatcher(
|
||||||
// This is a matcher on a method call,
|
// This is a matcher on a method call,
|
||||||
@@ -80,11 +79,9 @@ void DanglingOnTemporaryChecker::registerMatchers(MatchFinder *AstMatcher) {
|
|||||||
isFirstParty(),
|
isFirstParty(),
|
||||||
|
|
||||||
// and which is performed on a temporary,
|
// and which is performed on a temporary,
|
||||||
on(allOf(
|
on(allOf(unless(hasType(pointerType())), isTemporary(),
|
||||||
unless(hasType(pointerType())),
|
// but which is not `this`.
|
||||||
isTemporary(),
|
unless(cxxThisExpr()))),
|
||||||
// but which is not `this`.
|
|
||||||
unless(cxxThisExpr()))),
|
|
||||||
|
|
||||||
// and which is marked as no dangling on temporaries.
|
// and which is marked as no dangling on temporaries.
|
||||||
callee(cxxMethodDecl(noDanglingOnTemporaries())),
|
callee(cxxMethodDecl(noDanglingOnTemporaries())),
|
||||||
@@ -101,9 +98,7 @@ void DanglingOnTemporaryChecker::registerMatchers(MatchFinder *AstMatcher) {
|
|||||||
|
|
||||||
// This is the case where the call is not the direct parent, so we
|
// This is the case where the call is not the direct parent, so we
|
||||||
// get its child to know in which argument tree we are.
|
// get its child to know in which argument tree we are.
|
||||||
hasAncestor(expr(
|
hasAncestor(expr(hasParentCall, expr().bind("parentCallArg"))),
|
||||||
hasParentCall,
|
|
||||||
expr().bind("parentCallArg"))),
|
|
||||||
// To make it optional.
|
// To make it optional.
|
||||||
anything())),
|
anything())),
|
||||||
this);
|
this);
|
||||||
@@ -163,8 +158,7 @@ void DanglingOnTemporaryChecker::check(const MatchFinder::MatchResult &Result) {
|
|||||||
Result.Nodes.getNodeAs<CXXConstructExpr>("parentConstructExpr");
|
Result.Nodes.getNodeAs<CXXConstructExpr>("parentConstructExpr");
|
||||||
const CXXOperatorCallExpr *ParentOperatorCallExpr =
|
const CXXOperatorCallExpr *ParentOperatorCallExpr =
|
||||||
Result.Nodes.getNodeAs<CXXOperatorCallExpr>("parentOperatorCallExpr");
|
Result.Nodes.getNodeAs<CXXOperatorCallExpr>("parentOperatorCallExpr");
|
||||||
const Expr *ParentCallArg =
|
const Expr *ParentCallArg = Result.Nodes.getNodeAs<Expr>("parentCallArg");
|
||||||
Result.Nodes.getNodeAs<Expr>("parentCallArg");
|
|
||||||
|
|
||||||
// Just in case.
|
// Just in case.
|
||||||
if (!MemberCall) {
|
if (!MemberCall) {
|
||||||
@@ -180,12 +174,12 @@ void DanglingOnTemporaryChecker::check(const MatchFinder::MatchResult &Result) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// No default constructor so we can't construct it using if/else.
|
// No default constructor so we can't construct it using if/else.
|
||||||
auto FunctionEscapeData
|
auto FunctionEscapeData =
|
||||||
= ParentOperatorCallExpr
|
ParentOperatorCallExpr
|
||||||
? escapesFunction(ParentCallArg, ParentOperatorCallExpr)
|
? escapesFunction(ParentCallArg, ParentOperatorCallExpr)
|
||||||
: ParentCallExpr
|
: ParentCallExpr
|
||||||
? escapesFunction(ParentCallArg, ParentCallExpr)
|
? escapesFunction(ParentCallArg, ParentCallExpr)
|
||||||
: escapesFunction(ParentCallArg, ParentConstructExpr);
|
: escapesFunction(ParentCallArg, ParentConstructExpr);
|
||||||
|
|
||||||
// If there was an error in the escapesFunction call.
|
// If there was an error in the escapesFunction call.
|
||||||
if (std::error_code ec = FunctionEscapeData.getError()) {
|
if (std::error_code ec = FunctionEscapeData.getError()) {
|
||||||
@@ -193,11 +187,11 @@ void DanglingOnTemporaryChecker::check(const MatchFinder::MatchResult &Result) {
|
|||||||
// argument doesn't escape the function. Same for the case where we can't
|
// argument doesn't escape the function. Same for the case where we can't
|
||||||
// find the function declaration or if the function is builtin.
|
// find the function declaration or if the function is builtin.
|
||||||
if (static_cast<EscapesFunctionError>(ec.value()) ==
|
if (static_cast<EscapesFunctionError>(ec.value()) ==
|
||||||
EscapesFunctionError::FunctionIsVariadic ||
|
EscapesFunctionError::FunctionIsVariadic ||
|
||||||
static_cast<EscapesFunctionError>(ec.value()) ==
|
static_cast<EscapesFunctionError>(ec.value()) ==
|
||||||
EscapesFunctionError::FunctionDeclNotFound ||
|
EscapesFunctionError::FunctionDeclNotFound ||
|
||||||
static_cast<EscapesFunctionError>(ec.value()) ==
|
static_cast<EscapesFunctionError>(ec.value()) ==
|
||||||
EscapesFunctionError::FunctionIsBuiltin) {
|
EscapesFunctionError::FunctionIsBuiltin) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,5 +252,5 @@ void DanglingOnTemporaryChecker::check(const MatchFinder::MatchResult &Result) {
|
|||||||
diag(MemberCall->getExprLoc(), Error, DiagnosticIDs::Error)
|
diag(MemberCall->getExprLoc(), Error, DiagnosticIDs::Error)
|
||||||
<< MemberCall->getMethodDecl()->getName()
|
<< MemberCall->getMethodDecl()->getName()
|
||||||
<< MemberCall->getSourceRange();
|
<< MemberCall->getSourceRange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,14 +4,15 @@
|
|||||||
|
|
||||||
#include "DiagnosticsMatcher.h"
|
#include "DiagnosticsMatcher.h"
|
||||||
|
|
||||||
DiagnosticsMatcher::DiagnosticsMatcher(CompilerInstance& CI) :
|
DiagnosticsMatcher::DiagnosticsMatcher(CompilerInstance &CI)
|
||||||
#define CHECK(cls, name) cls ## _(name),
|
:
|
||||||
|
#define CHECK(cls, name) cls##_(name),
|
||||||
#include "Checks.inc"
|
#include "Checks.inc"
|
||||||
#undef CHECK
|
#undef CHECK
|
||||||
AstMatcher()
|
AstMatcher() {
|
||||||
{
|
#define CHECK(cls, name) \
|
||||||
#define CHECK(cls, name) cls ## _.registerMatchers(&AstMatcher); \
|
cls##_.registerMatchers(&AstMatcher); \
|
||||||
cls ## _.registerPPCallbacks(CI);
|
cls##_.registerPPCallbacks(CI);
|
||||||
#include "Checks.inc"
|
#include "Checks.inc"
|
||||||
#undef CHECK
|
#undef CHECK
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,12 +9,12 @@
|
|||||||
|
|
||||||
class DiagnosticsMatcher {
|
class DiagnosticsMatcher {
|
||||||
public:
|
public:
|
||||||
DiagnosticsMatcher(CompilerInstance& CI);
|
DiagnosticsMatcher(CompilerInstance &CI);
|
||||||
|
|
||||||
ASTConsumerPtr makeASTConsumer() { return AstMatcher.newASTConsumer(); }
|
ASTConsumerPtr makeASTConsumer() { return AstMatcher.newASTConsumer(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
#define CHECK(cls, name) cls cls ## _;
|
#define CHECK(cls, name) cls cls##_;
|
||||||
#include "Checks.inc"
|
#include "Checks.inc"
|
||||||
#undef CHECK
|
#undef CHECK
|
||||||
MatchFinder AstMatcher;
|
MatchFinder AstMatcher;
|
||||||
|
|||||||
@@ -5,17 +5,17 @@
|
|||||||
#include "ExplicitImplicitChecker.h"
|
#include "ExplicitImplicitChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void ExplicitImplicitChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void ExplicitImplicitChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(cxxConstructorDecl(isInterestingImplicitCtor(),
|
AstMatcher->addMatcher(
|
||||||
ofClass(allOf(isConcreteClass(),
|
cxxConstructorDecl(
|
||||||
decl().bind("class"))),
|
isInterestingImplicitCtor(),
|
||||||
unless(isMarkedImplicit()))
|
ofClass(allOf(isConcreteClass(), decl().bind("class"))),
|
||||||
.bind("ctor"),
|
unless(isMarkedImplicit()))
|
||||||
this);
|
.bind("ctor"),
|
||||||
|
this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExplicitImplicitChecker::check(
|
void ExplicitImplicitChecker::check(const MatchFinder::MatchResult &Result) {
|
||||||
const MatchFinder::MatchResult &Result) {
|
|
||||||
// We've already checked everything in the matcher, so we just have to report
|
// We've already checked everything in the matcher, so we just have to report
|
||||||
// the error.
|
// the error.
|
||||||
|
|
||||||
@@ -25,7 +25,9 @@ void ExplicitImplicitChecker::check(
|
|||||||
Result.Nodes.getNodeAs<CXXRecordDecl>("class");
|
Result.Nodes.getNodeAs<CXXRecordDecl>("class");
|
||||||
|
|
||||||
diag(Ctor->getLocation(), "bad implicit conversion constructor for %0",
|
diag(Ctor->getLocation(), "bad implicit conversion constructor for %0",
|
||||||
DiagnosticIDs::Error) << Declaration->getDeclName();
|
DiagnosticIDs::Error)
|
||||||
diag(Ctor->getLocation(), "consider adding the explicit keyword to the constructor",
|
<< Declaration->getDeclName();
|
||||||
|
diag(Ctor->getLocation(),
|
||||||
|
"consider adding the explicit keyword to the constructor",
|
||||||
DiagnosticIDs::Note);
|
DiagnosticIDs::Note);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,9 @@
|
|||||||
|
|
||||||
class ExplicitImplicitChecker : public BaseCheck {
|
class ExplicitImplicitChecker : public BaseCheck {
|
||||||
public:
|
public:
|
||||||
ExplicitImplicitChecker(StringRef CheckName,
|
ExplicitImplicitChecker(StringRef CheckName, ContextType *Context = nullptr)
|
||||||
ContextType *Context = nullptr)
|
: BaseCheck(CheckName, Context) {}
|
||||||
: BaseCheck(CheckName, Context) {}
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include "ExplicitOperatorBoolChecker.h"
|
#include "ExplicitOperatorBoolChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void ExplicitOperatorBoolChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void ExplicitOperatorBoolChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
// Older clang versions such as the ones used on the infra recognize these
|
// Older clang versions such as the ones used on the infra recognize these
|
||||||
// conversions as 'operator _Bool', but newer clang versions recognize these
|
// conversions as 'operator _Bool', but newer clang versions recognize these
|
||||||
// as 'operator bool'.
|
// as 'operator bool'.
|
||||||
@@ -26,8 +26,10 @@ void ExplicitOperatorBoolChecker::check(
|
|||||||
!ASTIsInSystemHeader(Method->getASTContext(), *Method) &&
|
!ASTIsInSystemHeader(Method->getASTContext(), *Method) &&
|
||||||
isInterestingDeclForImplicitConversion(Method)) {
|
isInterestingDeclForImplicitConversion(Method)) {
|
||||||
diag(Method->getLocStart(), "bad implicit conversion operator for %0",
|
diag(Method->getLocStart(), "bad implicit conversion operator for %0",
|
||||||
DiagnosticIDs::Error) << Clazz;
|
DiagnosticIDs::Error)
|
||||||
|
<< Clazz;
|
||||||
diag(Method->getLocStart(), "consider adding the explicit keyword to %0",
|
diag(Method->getLocStart(), "consider adding the explicit keyword to %0",
|
||||||
DiagnosticIDs::Note) << "'operator bool'";
|
DiagnosticIDs::Note)
|
||||||
|
<< "'operator bool'";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ class ExplicitOperatorBoolChecker : public BaseCheck {
|
|||||||
public:
|
public:
|
||||||
ExplicitOperatorBoolChecker(StringRef CheckName,
|
ExplicitOperatorBoolChecker(StringRef CheckName,
|
||||||
ContextType *Context = nullptr)
|
ContextType *Context = nullptr)
|
||||||
: BaseCheck(CheckName, Context) {}
|
: BaseCheck(CheckName, Context) {}
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,17 +5,15 @@
|
|||||||
#include "KungFuDeathGripChecker.h"
|
#include "KungFuDeathGripChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void KungFuDeathGripChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void KungFuDeathGripChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(varDecl(hasType(isRefPtr())).bind("decl"),
|
AstMatcher->addMatcher(varDecl(hasType(isRefPtr())).bind("decl"), this);
|
||||||
this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void KungFuDeathGripChecker::check(
|
void KungFuDeathGripChecker::check(const MatchFinder::MatchResult &Result) {
|
||||||
const MatchFinder::MatchResult &Result) {
|
const char *Error = "Unused \"kungFuDeathGrip\" %0 objects constructed from "
|
||||||
const char* Error =
|
"%1 are prohibited";
|
||||||
"Unused \"kungFuDeathGrip\" %0 objects constructed from %1 are prohibited";
|
const char *Note = "Please switch all accesses to this %0 to go through "
|
||||||
const char* Note =
|
"'%1', or explicitly pass '%1' to `mozilla::Unused`";
|
||||||
"Please switch all accesses to this %0 to go through '%1', or explicitly pass '%1' to `mozilla::Unused`";
|
|
||||||
|
|
||||||
const VarDecl *D = Result.Nodes.getNodeAs<VarDecl>("decl");
|
const VarDecl *D = Result.Nodes.getNodeAs<VarDecl>("decl");
|
||||||
if (D->isReferenced() || !D->hasLocalStorage() || !D->hasInit()) {
|
if (D->isReferenced() || !D->hasLocalStorage() || !D->hasInit()) {
|
||||||
@@ -64,17 +62,18 @@ void KungFuDeathGripChecker::check(
|
|||||||
const TagDecl *TD = E->getType()->getAsTagDecl();
|
const TagDecl *TD = E->getType()->getAsTagDecl();
|
||||||
if (TD && TD->getIdentifier()) {
|
if (TD && TD->getIdentifier()) {
|
||||||
static const char *IgnoreTypes[] = {
|
static const char *IgnoreTypes[] = {
|
||||||
"already_AddRefed",
|
"already_AddRefed",
|
||||||
"nsGetServiceByCID",
|
"nsGetServiceByCID",
|
||||||
"nsGetServiceByCIDWithError",
|
"nsGetServiceByCIDWithError",
|
||||||
"nsGetServiceByContractID",
|
"nsGetServiceByContractID",
|
||||||
"nsGetServiceByContractIDWithError",
|
"nsGetServiceByContractIDWithError",
|
||||||
"nsCreateInstanceByCID",
|
"nsCreateInstanceByCID",
|
||||||
"nsCreateInstanceByContractID",
|
"nsCreateInstanceByContractID",
|
||||||
"nsCreateInstanceFromFactory",
|
"nsCreateInstanceFromFactory",
|
||||||
};
|
};
|
||||||
|
|
||||||
for (uint32_t i = 0; i < sizeof(IgnoreTypes) / sizeof(IgnoreTypes[0]); ++i) {
|
for (uint32_t i = 0; i < sizeof(IgnoreTypes) / sizeof(IgnoreTypes[0]);
|
||||||
|
++i) {
|
||||||
if (TD->getName() == IgnoreTypes[i]) {
|
if (TD->getName() == IgnoreTypes[i]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -85,7 +84,7 @@ void KungFuDeathGripChecker::check(
|
|||||||
const char *ErrThing;
|
const char *ErrThing;
|
||||||
const char *NoteThing;
|
const char *NoteThing;
|
||||||
if (isa<MemberExpr>(E)) {
|
if (isa<MemberExpr>(E)) {
|
||||||
ErrThing = "members";
|
ErrThing = "members";
|
||||||
NoteThing = "member";
|
NoteThing = "member";
|
||||||
} else {
|
} else {
|
||||||
ErrThing = "temporary values";
|
ErrThing = "temporary values";
|
||||||
@@ -93,6 +92,8 @@ void KungFuDeathGripChecker::check(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// We cannot provide the note if we don't have an initializer
|
// We cannot provide the note if we don't have an initializer
|
||||||
diag(D->getLocStart(), Error, DiagnosticIDs::Error) << D->getType() << ErrThing;
|
diag(D->getLocStart(), Error, DiagnosticIDs::Error)
|
||||||
diag(E->getLocStart(), Note, DiagnosticIDs::Note) << NoteThing << getNameChecked(D);
|
<< D->getType() << ErrThing;
|
||||||
|
diag(E->getLocStart(), Note, DiagnosticIDs::Note)
|
||||||
|
<< NoteThing << getNameChecked(D);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,9 @@
|
|||||||
|
|
||||||
class KungFuDeathGripChecker : public BaseCheck {
|
class KungFuDeathGripChecker : public BaseCheck {
|
||||||
public:
|
public:
|
||||||
KungFuDeathGripChecker(StringRef CheckName,
|
KungFuDeathGripChecker(StringRef CheckName, ContextType *Context = nullptr)
|
||||||
ContextType *Context = nullptr)
|
: BaseCheck(CheckName, Context) {}
|
||||||
: BaseCheck(CheckName, Context) {}
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
#ifndef MemMoveAnnotation_h__
|
#ifndef MemMoveAnnotation_h__
|
||||||
#define MemMoveAnnotation_h__
|
#define MemMoveAnnotation_h__
|
||||||
|
|
||||||
#include "CustomTypeAnnotation.h"
|
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
#include "CustomTypeAnnotation.h"
|
||||||
#include "Utils.h"
|
#include "Utils.h"
|
||||||
|
|
||||||
class MemMoveAnnotation final : public CustomTypeAnnotation {
|
class MemMoveAnnotation final : public CustomTypeAnnotation {
|
||||||
@@ -24,30 +24,19 @@ protected:
|
|||||||
// This doesn't check that it's really ::std::pair and not
|
// This doesn't check that it's really ::std::pair and not
|
||||||
// ::std::something_else::pair, but should be good enough.
|
// ::std::something_else::pair, but should be good enough.
|
||||||
StringRef Name = getNameChecked(D);
|
StringRef Name = getNameChecked(D);
|
||||||
if (Name == "pair" ||
|
if (Name == "pair" || Name == "atomic" ||
|
||||||
Name == "atomic" ||
|
|
||||||
// libstdc++ specific names
|
// libstdc++ specific names
|
||||||
Name == "__atomic_base" ||
|
Name == "__atomic_base" || Name == "atomic_bool" ||
|
||||||
Name == "atomic_bool" ||
|
|
||||||
// MSVCRT specific names
|
// MSVCRT specific names
|
||||||
Name == "_Atomic_impl" ||
|
Name == "_Atomic_impl" || Name == "_Atomic_base" ||
|
||||||
Name == "_Atomic_base" ||
|
Name == "_Atomic_bool" || Name == "_Atomic_char" ||
|
||||||
Name == "_Atomic_bool" ||
|
Name == "_Atomic_schar" || Name == "_Atomic_uchar" ||
|
||||||
Name == "_Atomic_char" ||
|
Name == "_Atomic_char16_t" || Name == "_Atomic_char32_t" ||
|
||||||
Name == "_Atomic_schar" ||
|
Name == "_Atomic_wchar_t" || Name == "_Atomic_short" ||
|
||||||
Name == "_Atomic_uchar" ||
|
Name == "_Atomic_ushort" || Name == "_Atomic_int" ||
|
||||||
Name == "_Atomic_char16_t" ||
|
Name == "_Atomic_uint" || Name == "_Atomic_long" ||
|
||||||
Name == "_Atomic_char32_t" ||
|
Name == "_Atomic_ulong" || Name == "_Atomic_llong" ||
|
||||||
Name == "_Atomic_wchar_t" ||
|
Name == "_Atomic_ullong" || Name == "_Atomic_address") {
|
||||||
Name == "_Atomic_short" ||
|
|
||||||
Name == "_Atomic_ushort" ||
|
|
||||||
Name == "_Atomic_int" ||
|
|
||||||
Name == "_Atomic_uint" ||
|
|
||||||
Name == "_Atomic_long" ||
|
|
||||||
Name == "_Atomic_ulong" ||
|
|
||||||
Name == "_Atomic_llong" ||
|
|
||||||
Name == "_Atomic_ullong" ||
|
|
||||||
Name == "_Atomic_address") {
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
return "it is an stl-provided type not guaranteed to be memmove-able";
|
return "it is an stl-provided type not guaranteed to be memmove-able";
|
||||||
|
|||||||
@@ -2,16 +2,16 @@
|
|||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
#include "plugin.h"
|
|
||||||
#include "DiagnosticsMatcher.h"
|
#include "DiagnosticsMatcher.h"
|
||||||
|
#include "plugin.h"
|
||||||
#include "clang/Frontend/FrontendPluginRegistry.h"
|
#include "clang/Frontend/FrontendPluginRegistry.h"
|
||||||
|
|
||||||
class MozCheckAction : public PluginASTAction {
|
class MozCheckAction : public PluginASTAction {
|
||||||
public:
|
public:
|
||||||
ASTConsumerPtr CreateASTConsumer(CompilerInstance &CI,
|
ASTConsumerPtr CreateASTConsumer(CompilerInstance &CI,
|
||||||
StringRef FileName) override {
|
StringRef FileName) override {
|
||||||
void* Buffer = CI.getASTContext().Allocate<DiagnosticsMatcher>();
|
void *Buffer = CI.getASTContext().Allocate<DiagnosticsMatcher>();
|
||||||
auto Matcher = new(Buffer) DiagnosticsMatcher(CI);
|
auto Matcher = new (Buffer) DiagnosticsMatcher(CI);
|
||||||
return Matcher->makeASTConsumer();
|
return Matcher->makeASTConsumer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Register the MozillaTidyModule using this statically initialized variable.
|
// Register the MozillaTidyModule using this statically initialized variable.
|
||||||
static ClangTidyModuleRegistry::Add<MozillaModule> X("mozilla-module",
|
static ClangTidyModuleRegistry::Add<MozillaModule>
|
||||||
"Adds Mozilla lint checks.");
|
X("mozilla-module", "Adds Mozilla lint checks.");
|
||||||
|
|
||||||
} // namespace tidy
|
} // namespace tidy
|
||||||
} // namespace clang
|
} // namespace clang
|
||||||
|
|||||||
@@ -5,23 +5,22 @@
|
|||||||
#include "MustOverrideChecker.h"
|
#include "MustOverrideChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void MustOverrideChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void MustOverrideChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(cxxRecordDecl(isDefinition()).bind("class"), this);
|
AstMatcher->addMatcher(cxxRecordDecl(isDefinition()).bind("class"), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MustOverrideChecker::registerPPCallbacks(CompilerInstance& CI) {
|
void MustOverrideChecker::registerPPCallbacks(CompilerInstance &CI) {
|
||||||
this->CI = &CI;
|
this->CI = &CI;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MustOverrideChecker::check(
|
void MustOverrideChecker::check(const MatchFinder::MatchResult &Result) {
|
||||||
const MatchFinder::MatchResult &Result) {
|
|
||||||
auto D = Result.Nodes.getNodeAs<CXXRecordDecl>("class");
|
auto D = Result.Nodes.getNodeAs<CXXRecordDecl>("class");
|
||||||
|
|
||||||
// Look through all of our immediate bases to find methods that need to be
|
// Look through all of our immediate bases to find methods that need to be
|
||||||
// overridden
|
// overridden
|
||||||
typedef std::vector<CXXMethodDecl *> OverridesVector;
|
typedef std::vector<CXXMethodDecl *> OverridesVector;
|
||||||
OverridesVector MustOverrides;
|
OverridesVector MustOverrides;
|
||||||
for (const auto& Base : D->bases()) {
|
for (const auto &Base : D->bases()) {
|
||||||
// The base is either a class (CXXRecordDecl) or it's a templated class...
|
// The base is either a class (CXXRecordDecl) or it's a templated class...
|
||||||
CXXRecordDecl *Parent = Base.getType()
|
CXXRecordDecl *Parent = Base.getType()
|
||||||
.getDesugaredType(D->getASTContext())
|
.getDesugaredType(D->getASTContext())
|
||||||
@@ -34,15 +33,15 @@ void MustOverrideChecker::check(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Parent = Parent->getDefinition();
|
Parent = Parent->getDefinition();
|
||||||
for (const auto& M : Parent->methods()) {
|
for (const auto &M : Parent->methods()) {
|
||||||
if (hasCustomAnnotation(M, "moz_must_override"))
|
if (hasCustomAnnotation(M, "moz_must_override"))
|
||||||
MustOverrides.push_back(M);
|
MustOverrides.push_back(M);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& O : MustOverrides) {
|
for (auto &O : MustOverrides) {
|
||||||
bool Overridden = false;
|
bool Overridden = false;
|
||||||
for (const auto& M : D->methods()) {
|
for (const auto &M : D->methods()) {
|
||||||
// The way that Clang checks if a method M overrides its parent method
|
// The way that Clang checks if a method M overrides its parent method
|
||||||
// is if the method has the same name but would not overload.
|
// is if the method has the same name but would not overload.
|
||||||
if (getNameChecked(M) == getNameChecked(O) &&
|
if (getNameChecked(M) == getNameChecked(O) &&
|
||||||
@@ -52,9 +51,8 @@ void MustOverrideChecker::check(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!Overridden) {
|
if (!Overridden) {
|
||||||
diag(D->getLocation(), "%0 must override %1",
|
diag(D->getLocation(), "%0 must override %1", DiagnosticIDs::Error)
|
||||||
DiagnosticIDs::Error) << D->getDeclName()
|
<< D->getDeclName() << O->getDeclName();
|
||||||
<< O->getDeclName();
|
|
||||||
diag(O->getLocation(), "function to override is here",
|
diag(O->getLocation(), "function to override is here",
|
||||||
DiagnosticIDs::Note);
|
DiagnosticIDs::Note);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,15 +9,14 @@
|
|||||||
|
|
||||||
class MustOverrideChecker : public BaseCheck {
|
class MustOverrideChecker : public BaseCheck {
|
||||||
public:
|
public:
|
||||||
MustOverrideChecker(StringRef CheckName,
|
MustOverrideChecker(StringRef CheckName, ContextType *Context = nullptr)
|
||||||
ContextType *Context = nullptr)
|
: BaseCheck(CheckName, Context), CI(nullptr) {}
|
||||||
: BaseCheck(CheckName, Context), CI(nullptr) {}
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
void registerPPCallbacks(CompilerInstance &CI) override;
|
||||||
void registerPPCallbacks(CompilerInstance& CI) override;
|
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const CompilerInstance* CI;
|
const CompilerInstance *CI;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -5,20 +5,22 @@
|
|||||||
#include "MustReturnFromCallerChecker.h"
|
#include "MustReturnFromCallerChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void MustReturnFromCallerChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void MustReturnFromCallerChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
// Look for a call to a MOZ_MUST_RETURN_FROM_CALLER function
|
// Look for a call to a MOZ_MUST_RETURN_FROM_CALLER function
|
||||||
AstMatcher->addMatcher(callExpr(callee(functionDecl(isMozMustReturnFromCaller())),
|
AstMatcher->addMatcher(
|
||||||
anyOf(hasAncestor(lambdaExpr().bind("containing-lambda")),
|
callExpr(callee(functionDecl(isMozMustReturnFromCaller())),
|
||||||
hasAncestor(functionDecl().bind("containing-func")))).bind("call"),
|
anyOf(hasAncestor(lambdaExpr().bind("containing-lambda")),
|
||||||
this);
|
hasAncestor(functionDecl().bind("containing-func"))))
|
||||||
|
.bind("call"),
|
||||||
|
this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MustReturnFromCallerChecker::check(
|
void MustReturnFromCallerChecker::check(
|
||||||
const MatchFinder::MatchResult& Result) {
|
const MatchFinder::MatchResult &Result) {
|
||||||
const auto *ContainingLambda =
|
const auto *ContainingLambda =
|
||||||
Result.Nodes.getNodeAs<LambdaExpr>("containing-lambda");
|
Result.Nodes.getNodeAs<LambdaExpr>("containing-lambda");
|
||||||
const auto *ContainingFunc =
|
const auto *ContainingFunc =
|
||||||
Result.Nodes.getNodeAs<FunctionDecl>("containing-func");
|
Result.Nodes.getNodeAs<FunctionDecl>("containing-func");
|
||||||
const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
|
const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
|
||||||
|
|
||||||
Stmt *Body = nullptr;
|
Stmt *Body = nullptr;
|
||||||
@@ -34,7 +36,7 @@ void MustReturnFromCallerChecker::check(
|
|||||||
// Generate the CFG for the enclosing function or decl.
|
// Generate the CFG for the enclosing function or decl.
|
||||||
CFG::BuildOptions Options;
|
CFG::BuildOptions Options;
|
||||||
std::unique_ptr<CFG> TheCFG =
|
std::unique_ptr<CFG> TheCFG =
|
||||||
CFG::buildCFG(nullptr, Body, Result.Context, Options);
|
CFG::buildCFG(nullptr, Body, Result.Context, Options);
|
||||||
if (!TheCFG) {
|
if (!TheCFG) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -52,10 +54,9 @@ void MustReturnFromCallerChecker::check(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool MustReturnFromCallerChecker::immediatelyReturns(
|
||||||
MustReturnFromCallerChecker::immediatelyReturns(RecurseGuard<const CFGBlock *> Block,
|
RecurseGuard<const CFGBlock *> Block, ASTContext *TheContext,
|
||||||
ASTContext *TheContext,
|
size_t FromIdx) {
|
||||||
size_t FromIdx) {
|
|
||||||
if (Block.isRepeat()) {
|
if (Block.isRepeat()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -73,8 +74,7 @@ MustReturnFromCallerChecker::immediatelyReturns(RecurseGuard<const CFGBlock *> B
|
|||||||
// It is also, of course, OK to look at a ReturnStmt.
|
// It is also, of course, OK to look at a ReturnStmt.
|
||||||
if (isa<ReturnStmt>(AfterTrivials) ||
|
if (isa<ReturnStmt>(AfterTrivials) ||
|
||||||
isa<CXXConstructExpr>(AfterTrivials) ||
|
isa<CXXConstructExpr>(AfterTrivials) ||
|
||||||
isa<DeclRefExpr>(AfterTrivials) ||
|
isa<DeclRefExpr>(AfterTrivials) || isa<MemberExpr>(AfterTrivials)) {
|
||||||
isa<MemberExpr>(AfterTrivials)) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +83,8 @@ MustReturnFromCallerChecker::immediatelyReturns(RecurseGuard<const CFGBlock *> B
|
|||||||
// to be MOZ_MAY_CALL_AFTER_MUST_RETURN (like operator T*()).
|
// to be MOZ_MAY_CALL_AFTER_MUST_RETURN (like operator T*()).
|
||||||
if (auto CE = dyn_cast<CallExpr>(AfterTrivials)) {
|
if (auto CE = dyn_cast<CallExpr>(AfterTrivials)) {
|
||||||
auto Callee = CE->getDirectCallee();
|
auto Callee = CE->getDirectCallee();
|
||||||
if (Callee && hasCustomAnnotation(Callee, "moz_may_call_after_must_return")) {
|
if (Callee &&
|
||||||
|
hasCustomAnnotation(Callee, "moz_may_call_after_must_return")) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,22 +5,22 @@
|
|||||||
#ifndef MustReturnFromCallerChecker_h__
|
#ifndef MustReturnFromCallerChecker_h__
|
||||||
#define MustReturnFromCallerChecker_h__
|
#define MustReturnFromCallerChecker_h__
|
||||||
|
|
||||||
#include "plugin.h"
|
|
||||||
#include "Utils.h"
|
|
||||||
#include "RecurseGuard.h"
|
#include "RecurseGuard.h"
|
||||||
#include "StmtToBlockMap.h"
|
#include "StmtToBlockMap.h"
|
||||||
|
#include "Utils.h"
|
||||||
|
#include "plugin.h"
|
||||||
|
|
||||||
class MustReturnFromCallerChecker : public BaseCheck {
|
class MustReturnFromCallerChecker : public BaseCheck {
|
||||||
public:
|
public:
|
||||||
MustReturnFromCallerChecker(StringRef CheckName,
|
MustReturnFromCallerChecker(StringRef CheckName,
|
||||||
ContextType *Context = nullptr)
|
ContextType *Context = nullptr)
|
||||||
: BaseCheck(CheckName, Context) {}
|
: BaseCheck(CheckName, Context) {}
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool immediatelyReturns(RecurseGuard<const CFGBlock *> Block,
|
bool immediatelyReturns(RecurseGuard<const CFGBlock *> Block,
|
||||||
ASTContext *TheContext,
|
ASTContext *TheContext, size_t FromIdx);
|
||||||
size_t FromIdx);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -9,23 +9,23 @@
|
|||||||
CustomTypeAnnotation MustUse =
|
CustomTypeAnnotation MustUse =
|
||||||
CustomTypeAnnotation("moz_must_use_type", "must-use");
|
CustomTypeAnnotation("moz_must_use_type", "must-use");
|
||||||
|
|
||||||
void MustUseChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void MustUseChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(switchCase().bind("switchcase"), this);
|
AstMatcher->addMatcher(switchCase().bind("switchcase"), this);
|
||||||
AstMatcher->addMatcher(compoundStmt().bind("compound"), this);
|
AstMatcher->addMatcher(compoundStmt().bind("compound"), this);
|
||||||
AstMatcher->addMatcher(ifStmt().bind("if"), this);
|
AstMatcher->addMatcher(ifStmt().bind("if"), this);
|
||||||
AstMatcher->addMatcher(whileStmt().bind("while"), this);
|
AstMatcher->addMatcher(whileStmt().bind("while"), this);
|
||||||
AstMatcher->addMatcher(doStmt().bind("do"), this);
|
AstMatcher->addMatcher(doStmt().bind("do"), this);
|
||||||
AstMatcher->addMatcher(forStmt().bind("for"), this);
|
AstMatcher->addMatcher(forStmt().bind("for"), this);
|
||||||
AstMatcher->addMatcher(binaryOperator(binaryCommaOperator()).bind("bin"), this);
|
AstMatcher->addMatcher(binaryOperator(binaryCommaOperator()).bind("bin"),
|
||||||
|
this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MustUseChecker::check(
|
void MustUseChecker::check(const MatchFinder::MatchResult &Result) {
|
||||||
const MatchFinder::MatchResult &Result) {
|
|
||||||
if (auto SC = Result.Nodes.getNodeAs<SwitchCase>("switchcase")) {
|
if (auto SC = Result.Nodes.getNodeAs<SwitchCase>("switchcase")) {
|
||||||
handleUnusedExprResult(SC->getSubStmt());
|
handleUnusedExprResult(SC->getSubStmt());
|
||||||
}
|
}
|
||||||
if (auto C = Result.Nodes.getNodeAs<CompoundStmt>("compound")) {
|
if (auto C = Result.Nodes.getNodeAs<CompoundStmt>("compound")) {
|
||||||
for (const auto& S : C->body()) {
|
for (const auto &S : C->body()) {
|
||||||
handleUnusedExprResult(S);
|
handleUnusedExprResult(S);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -56,7 +56,8 @@ void MustUseChecker::handleUnusedExprResult(const Stmt *Statement) {
|
|||||||
QualType T = E->getType();
|
QualType T = E->getType();
|
||||||
if (MustUse.hasEffectiveAnnotation(T) && !isIgnoredExprForMustUse(E)) {
|
if (MustUse.hasEffectiveAnnotation(T) && !isIgnoredExprForMustUse(E)) {
|
||||||
diag(E->getLocStart(), "Unused value of must-use type %0",
|
diag(E->getLocStart(), "Unused value of must-use type %0",
|
||||||
DiagnosticIDs::Error) << T;
|
DiagnosticIDs::Error)
|
||||||
|
<< T;
|
||||||
MustUse.dumpAnnotationReason(*this, T, E->getLocStart());
|
MustUse.dumpAnnotationReason(*this, T, E->getLocStart());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,9 @@
|
|||||||
|
|
||||||
class MustUseChecker : public BaseCheck {
|
class MustUseChecker : public BaseCheck {
|
||||||
public:
|
public:
|
||||||
MustUseChecker(StringRef CheckName,
|
MustUseChecker(StringRef CheckName, ContextType *Context = nullptr)
|
||||||
ContextType *Context = nullptr)
|
: BaseCheck(CheckName, Context) {}
|
||||||
: BaseCheck(CheckName, Context) {}
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include "NaNExprChecker.h"
|
#include "NaNExprChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void NaNExprChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void NaNExprChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(
|
AstMatcher->addMatcher(
|
||||||
binaryOperator(
|
binaryOperator(
|
||||||
allOf(binaryEqualityOperator(),
|
allOf(binaryEqualityOperator(),
|
||||||
@@ -18,22 +18,21 @@ void NaNExprChecker::registerMatchers(MatchFinder* AstMatcher) {
|
|||||||
this);
|
this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NaNExprChecker::check(
|
void NaNExprChecker::check(const MatchFinder::MatchResult &Result) {
|
||||||
const MatchFinder::MatchResult &Result) {
|
|
||||||
if (!Result.Context->getLangOpts().CPlusPlus) {
|
if (!Result.Context->getLangOpts().CPlusPlus) {
|
||||||
// mozilla::IsNaN is not usable in C, so there is no point in issuing these
|
// mozilla::IsNaN is not usable in C, so there is no point in issuing these
|
||||||
// warnings.
|
// warnings.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const BinaryOperator *Expression = Result.Nodes.getNodeAs<BinaryOperator>(
|
const BinaryOperator *Expression =
|
||||||
"node");
|
Result.Nodes.getNodeAs<BinaryOperator>("node");
|
||||||
const DeclRefExpr *LHS = Result.Nodes.getNodeAs<DeclRefExpr>("lhs");
|
const DeclRefExpr *LHS = Result.Nodes.getNodeAs<DeclRefExpr>("lhs");
|
||||||
const DeclRefExpr *RHS = Result.Nodes.getNodeAs<DeclRefExpr>("rhs");
|
const DeclRefExpr *RHS = Result.Nodes.getNodeAs<DeclRefExpr>("rhs");
|
||||||
const ImplicitCastExpr *LHSExpr = dyn_cast<ImplicitCastExpr>(
|
const ImplicitCastExpr *LHSExpr =
|
||||||
Expression->getLHS());
|
dyn_cast<ImplicitCastExpr>(Expression->getLHS());
|
||||||
const ImplicitCastExpr *RHSExpr = dyn_cast<ImplicitCastExpr>(
|
const ImplicitCastExpr *RHSExpr =
|
||||||
Expression->getRHS());
|
dyn_cast<ImplicitCastExpr>(Expression->getRHS());
|
||||||
// The AST subtree that we are looking for will look like this:
|
// The AST subtree that we are looking for will look like this:
|
||||||
// -BinaryOperator ==/!=
|
// -BinaryOperator ==/!=
|
||||||
// |-ImplicitCastExpr LValueToRValue
|
// |-ImplicitCastExpr LValueToRValue
|
||||||
@@ -47,8 +46,9 @@ void NaNExprChecker::check(
|
|||||||
std::distance(LHSExpr->child_begin(), LHSExpr->child_end()) == 1 &&
|
std::distance(LHSExpr->child_begin(), LHSExpr->child_end()) == 1 &&
|
||||||
std::distance(RHSExpr->child_begin(), RHSExpr->child_end()) == 1 &&
|
std::distance(RHSExpr->child_begin(), RHSExpr->child_end()) == 1 &&
|
||||||
*LHSExpr->child_begin() == LHS && *RHSExpr->child_begin() == RHS) {
|
*LHSExpr->child_begin() == LHS && *RHSExpr->child_begin() == RHS) {
|
||||||
diag(Expression->getLocStart(), "comparing a floating point value to itself for "
|
diag(Expression->getLocStart(),
|
||||||
"NaN checking can lead to incorrect results",
|
"comparing a floating point value to itself for "
|
||||||
|
"NaN checking can lead to incorrect results",
|
||||||
DiagnosticIDs::Error);
|
DiagnosticIDs::Error);
|
||||||
diag(Expression->getLocStart(), "consider using mozilla::IsNaN instead",
|
diag(Expression->getLocStart(), "consider using mozilla::IsNaN instead",
|
||||||
DiagnosticIDs::Note);
|
DiagnosticIDs::Note);
|
||||||
|
|||||||
@@ -9,10 +9,9 @@
|
|||||||
|
|
||||||
class NaNExprChecker : public BaseCheck {
|
class NaNExprChecker : public BaseCheck {
|
||||||
public:
|
public:
|
||||||
NaNExprChecker(StringRef CheckName,
|
NaNExprChecker(StringRef CheckName, ContextType *Context = nullptr)
|
||||||
ContextType *Context = nullptr)
|
: BaseCheck(CheckName, Context) {}
|
||||||
: BaseCheck(CheckName, Context) {}
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include "NeedsNoVTableTypeChecker.h"
|
#include "NeedsNoVTableTypeChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void NeedsNoVTableTypeChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void NeedsNoVTableTypeChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(
|
AstMatcher->addMatcher(
|
||||||
classTemplateSpecializationDecl(
|
classTemplateSpecializationDecl(
|
||||||
allOf(hasAnyTemplateArgument(refersToType(hasVTable())),
|
allOf(hasAnyTemplateArgument(refersToType(hasVTable())),
|
||||||
@@ -14,8 +14,7 @@ void NeedsNoVTableTypeChecker::registerMatchers(MatchFinder* AstMatcher) {
|
|||||||
this);
|
this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NeedsNoVTableTypeChecker::check(
|
void NeedsNoVTableTypeChecker::check(const MatchFinder::MatchResult &Result) {
|
||||||
const MatchFinder::MatchResult &Result) {
|
|
||||||
const ClassTemplateSpecializationDecl *Specialization =
|
const ClassTemplateSpecializationDecl *Specialization =
|
||||||
Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("node");
|
Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("node");
|
||||||
|
|
||||||
@@ -32,9 +31,9 @@ void NeedsNoVTableTypeChecker::check(
|
|||||||
|
|
||||||
diag(Specialization->getLocStart(),
|
diag(Specialization->getLocStart(),
|
||||||
"%0 cannot be instantiated because %1 has a VTable",
|
"%0 cannot be instantiated because %1 has a VTable",
|
||||||
DiagnosticIDs::Error) << Specialization
|
DiagnosticIDs::Error)
|
||||||
<< Offender;
|
<< Specialization << Offender;
|
||||||
diag(Specialization->getPointOfInstantiation(),
|
diag(Specialization->getPointOfInstantiation(),
|
||||||
"bad instantiation of %0 requested here",
|
"bad instantiation of %0 requested here", DiagnosticIDs::Note)
|
||||||
DiagnosticIDs::Note) << Specialization;
|
<< Specialization;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,9 @@
|
|||||||
|
|
||||||
class NeedsNoVTableTypeChecker : public BaseCheck {
|
class NeedsNoVTableTypeChecker : public BaseCheck {
|
||||||
public:
|
public:
|
||||||
NeedsNoVTableTypeChecker(StringRef CheckName,
|
NeedsNoVTableTypeChecker(StringRef CheckName, ContextType *Context = nullptr)
|
||||||
ContextType *Context = nullptr)
|
: BaseCheck(CheckName, Context) {}
|
||||||
: BaseCheck(CheckName, Context) {}
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,11 @@
|
|||||||
#include "NoAddRefReleaseOnReturnChecker.h"
|
#include "NoAddRefReleaseOnReturnChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void NoAddRefReleaseOnReturnChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void NoAddRefReleaseOnReturnChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
// Look for all of the calls to AddRef() or Release()
|
// Look for all of the calls to AddRef() or Release()
|
||||||
AstMatcher->addMatcher(memberExpr(isAddRefOrRelease(), hasParent(callExpr())).bind("member"),
|
AstMatcher->addMatcher(
|
||||||
this);
|
memberExpr(isAddRefOrRelease(), hasParent(callExpr())).bind("member"),
|
||||||
|
this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NoAddRefReleaseOnReturnChecker::check(
|
void NoAddRefReleaseOnReturnChecker::check(
|
||||||
@@ -24,8 +25,7 @@ void NoAddRefReleaseOnReturnChecker::check(
|
|||||||
diag(Call->getLocStart(),
|
diag(Call->getLocStart(),
|
||||||
"%1 cannot be called on the return value of %0",
|
"%1 cannot be called on the return value of %0",
|
||||||
DiagnosticIDs::Error)
|
DiagnosticIDs::Error)
|
||||||
<< Callee
|
<< Callee << dyn_cast<CXXMethodDecl>(Member->getMemberDecl());
|
||||||
<< dyn_cast<CXXMethodDecl>(Member->getMemberDecl());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ class NoAddRefReleaseOnReturnChecker : public BaseCheck {
|
|||||||
public:
|
public:
|
||||||
NoAddRefReleaseOnReturnChecker(StringRef CheckName,
|
NoAddRefReleaseOnReturnChecker(StringRef CheckName,
|
||||||
ContextType *Context = nullptr)
|
ContextType *Context = nullptr)
|
||||||
: BaseCheck(CheckName, Context) {}
|
: BaseCheck(CheckName, Context) {}
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,19 +5,17 @@
|
|||||||
#include "NoAutoTypeChecker.h"
|
#include "NoAutoTypeChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void NoAutoTypeChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void NoAutoTypeChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(varDecl(hasType(autoNonAutoableType())).bind("node"),
|
AstMatcher->addMatcher(varDecl(hasType(autoNonAutoableType())).bind("node"),
|
||||||
this);
|
this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NoAutoTypeChecker::check(
|
void NoAutoTypeChecker::check(const MatchFinder::MatchResult &Result) {
|
||||||
const MatchFinder::MatchResult &Result) {
|
|
||||||
const VarDecl *D = Result.Nodes.getNodeAs<VarDecl>("node");
|
const VarDecl *D = Result.Nodes.getNodeAs<VarDecl>("node");
|
||||||
|
|
||||||
diag(D->getLocation(),
|
diag(D->getLocation(), "Cannot use auto to declare a variable of type %0",
|
||||||
"Cannot use auto to declare a variable of type %0",
|
DiagnosticIDs::Error)
|
||||||
DiagnosticIDs::Error) << D->getType();
|
<< D->getType();
|
||||||
diag(D->getLocation(),
|
diag(D->getLocation(), "Please write out this type explicitly",
|
||||||
"Please write out this type explicitly",
|
|
||||||
DiagnosticIDs::Note);
|
DiagnosticIDs::Note);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,9 @@
|
|||||||
|
|
||||||
class NoAutoTypeChecker : public BaseCheck {
|
class NoAutoTypeChecker : public BaseCheck {
|
||||||
public:
|
public:
|
||||||
NoAutoTypeChecker(StringRef CheckName,
|
NoAutoTypeChecker(StringRef CheckName, ContextType *Context = nullptr)
|
||||||
ContextType *Context = nullptr)
|
: BaseCheck(CheckName, Context) {}
|
||||||
: BaseCheck(CheckName, Context) {}
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include "NoDuplicateRefCntMemberChecker.h"
|
#include "NoDuplicateRefCntMemberChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void NoDuplicateRefCntMemberChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void NoDuplicateRefCntMemberChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(cxxRecordDecl().bind("decl"), this);
|
AstMatcher->addMatcher(cxxRecordDecl().bind("decl"), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,31 +34,28 @@ void NoDuplicateRefCntMemberChecker::check(
|
|||||||
if (BaseRefCntMember) {
|
if (BaseRefCntMember) {
|
||||||
if (RefCntMember) {
|
if (RefCntMember) {
|
||||||
// We have an mRefCnt, and superclass has an mRefCnt
|
// We have an mRefCnt, and superclass has an mRefCnt
|
||||||
const char* Error =
|
const char *Error = "Refcounted record %0 has multiple mRefCnt members";
|
||||||
"Refcounted record %0 has multiple mRefCnt members";
|
const char *Note1 = "Superclass %0 also has an mRefCnt member";
|
||||||
const char* Note1 =
|
const char *Note2 =
|
||||||
"Superclass %0 also has an mRefCnt member";
|
|
||||||
const char* Note2 =
|
|
||||||
"Consider using the _INHERITED macros for AddRef and Release here";
|
"Consider using the _INHERITED macros for AddRef and Release here";
|
||||||
|
|
||||||
diag(D->getLocStart(), Error, DiagnosticIDs::Error) << D;
|
diag(D->getLocStart(), Error, DiagnosticIDs::Error) << D;
|
||||||
diag(BaseRefCntMember->getLocStart(), Note1, DiagnosticIDs::Note)
|
diag(BaseRefCntMember->getLocStart(), Note1, DiagnosticIDs::Note)
|
||||||
<< BaseRefCntMember->getParent();
|
<< BaseRefCntMember->getParent();
|
||||||
diag(RefCntMember->getLocStart(), Note2, DiagnosticIDs::Note);
|
diag(RefCntMember->getLocStart(), Note2, DiagnosticIDs::Note);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (FoundRefCntBase) {
|
if (FoundRefCntBase) {
|
||||||
const char* Error =
|
const char *Error = "Refcounted record %0 has multiple superclasses "
|
||||||
"Refcounted record %0 has multiple superclasses with mRefCnt members";
|
"with mRefCnt members";
|
||||||
const char* Note =
|
const char *Note = "Superclass %0 has an mRefCnt member";
|
||||||
"Superclass %0 has an mRefCnt member";
|
|
||||||
|
|
||||||
// superclass has mRefCnt, and another superclass also has an mRefCnt
|
// superclass has mRefCnt, and another superclass also has an mRefCnt
|
||||||
diag(D->getLocStart(), Error, DiagnosticIDs::Error) << D;
|
diag(D->getLocStart(), Error, DiagnosticIDs::Error) << D;
|
||||||
diag(BaseRefCntMember->getLocStart(), Note, DiagnosticIDs::Note)
|
diag(BaseRefCntMember->getLocStart(), Note, DiagnosticIDs::Note)
|
||||||
<< BaseRefCntMember->getParent();
|
<< BaseRefCntMember->getParent();
|
||||||
diag(FoundRefCntBase->getLocStart(), Note, DiagnosticIDs::Note)
|
diag(FoundRefCntBase->getLocStart(), Note, DiagnosticIDs::Note)
|
||||||
<< FoundRefCntBase->getParent();
|
<< FoundRefCntBase->getParent();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Record that we've found a base with a mRefCnt member
|
// Record that we've found a base with a mRefCnt member
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ class NoDuplicateRefCntMemberChecker : public BaseCheck {
|
|||||||
public:
|
public:
|
||||||
NoDuplicateRefCntMemberChecker(StringRef CheckName,
|
NoDuplicateRefCntMemberChecker(StringRef CheckName,
|
||||||
ContextType *Context = nullptr)
|
ContextType *Context = nullptr)
|
||||||
: BaseCheck(CheckName, Context) {}
|
: BaseCheck(CheckName, Context) {}
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,10 @@
|
|||||||
#include "NoExplicitMoveConstructorChecker.h"
|
#include "NoExplicitMoveConstructorChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void NoExplicitMoveConstructorChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void NoExplicitMoveConstructorChecker::registerMatchers(
|
||||||
|
MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(
|
AstMatcher->addMatcher(
|
||||||
cxxConstructorDecl(isExplicitMoveConstructor()).bind("node"),
|
cxxConstructorDecl(isExplicitMoveConstructor()).bind("node"), this);
|
||||||
this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NoExplicitMoveConstructorChecker::check(
|
void NoExplicitMoveConstructorChecker::check(
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ class NoExplicitMoveConstructorChecker : public BaseCheck {
|
|||||||
public:
|
public:
|
||||||
NoExplicitMoveConstructorChecker(StringRef CheckName,
|
NoExplicitMoveConstructorChecker(StringRef CheckName,
|
||||||
ContextType *Context = nullptr)
|
ContextType *Context = nullptr)
|
||||||
: BaseCheck(CheckName, Context) {}
|
: BaseCheck(CheckName, Context) {}
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -7,21 +7,18 @@
|
|||||||
|
|
||||||
MemMoveAnnotation NonMemMovable = MemMoveAnnotation();
|
MemMoveAnnotation NonMemMovable = MemMoveAnnotation();
|
||||||
|
|
||||||
void NonMemMovableMemberChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void NonMemMovableMemberChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
// Handle non-mem-movable members
|
// Handle non-mem-movable members
|
||||||
AstMatcher->addMatcher(
|
AstMatcher->addMatcher(cxxRecordDecl(needsMemMovableMembers()).bind("decl"),
|
||||||
cxxRecordDecl(needsMemMovableMembers())
|
this);
|
||||||
.bind("decl"),
|
|
||||||
this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NonMemMovableMemberChecker::check(
|
void NonMemMovableMemberChecker::check(const MatchFinder::MatchResult &Result) {
|
||||||
const MatchFinder::MatchResult &Result) {
|
const char *Error =
|
||||||
const char* Error =
|
|
||||||
"class %0 cannot have non-memmovable member %1 of type %2";
|
"class %0 cannot have non-memmovable member %1 of type %2";
|
||||||
|
|
||||||
// Get the specialization
|
// Get the specialization
|
||||||
const CXXRecordDecl* Declaration =
|
const CXXRecordDecl *Declaration =
|
||||||
Result.Nodes.getNodeAs<CXXRecordDecl>("decl");
|
Result.Nodes.getNodeAs<CXXRecordDecl>("decl");
|
||||||
|
|
||||||
// Report an error for every member which is non-memmovable
|
// Report an error for every member which is non-memmovable
|
||||||
@@ -29,10 +26,9 @@ void NonMemMovableMemberChecker::check(
|
|||||||
QualType Type = Field->getType();
|
QualType Type = Field->getType();
|
||||||
if (NonMemMovable.hasEffectiveAnnotation(Type)) {
|
if (NonMemMovable.hasEffectiveAnnotation(Type)) {
|
||||||
diag(Field->getLocation(), Error, DiagnosticIDs::Error)
|
diag(Field->getLocation(), Error, DiagnosticIDs::Error)
|
||||||
<< Declaration
|
<< Declaration << Field << Type;
|
||||||
<< Field
|
NonMemMovable.dumpAnnotationReason(*this, Type,
|
||||||
<< Type;
|
Declaration->getLocation());
|
||||||
NonMemMovable.dumpAnnotationReason(*this, Type, Declaration->getLocation());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ class NonMemMovableMemberChecker : public BaseCheck {
|
|||||||
public:
|
public:
|
||||||
NonMemMovableMemberChecker(StringRef CheckName,
|
NonMemMovableMemberChecker(StringRef CheckName,
|
||||||
ContextType *Context = nullptr)
|
ContextType *Context = nullptr)
|
||||||
: BaseCheck(CheckName, Context) {}
|
: BaseCheck(CheckName, Context) {}
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,8 @@
|
|||||||
#include "NonMemMovableTemplateArgChecker.h"
|
#include "NonMemMovableTemplateArgChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void NonMemMovableTemplateArgChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void NonMemMovableTemplateArgChecker::registerMatchers(
|
||||||
|
MatchFinder *AstMatcher) {
|
||||||
// Handle non-mem-movable template specializations
|
// Handle non-mem-movable template specializations
|
||||||
AstMatcher->addMatcher(
|
AstMatcher->addMatcher(
|
||||||
classTemplateSpecializationDecl(
|
classTemplateSpecializationDecl(
|
||||||
@@ -17,10 +18,9 @@ void NonMemMovableTemplateArgChecker::registerMatchers(MatchFinder* AstMatcher)
|
|||||||
|
|
||||||
void NonMemMovableTemplateArgChecker::check(
|
void NonMemMovableTemplateArgChecker::check(
|
||||||
const MatchFinder::MatchResult &Result) {
|
const MatchFinder::MatchResult &Result) {
|
||||||
const char* Error =
|
const char *Error =
|
||||||
"Cannot instantiate %0 with non-memmovable template argument %1";
|
"Cannot instantiate %0 with non-memmovable template argument %1";
|
||||||
const char* Note =
|
const char *Note = "instantiation of %0 requested here";
|
||||||
"instantiation of %0 requested here";
|
|
||||||
|
|
||||||
// Get the specialization
|
// Get the specialization
|
||||||
const ClassTemplateSpecializationDecl *Specialization =
|
const ClassTemplateSpecializationDecl *Specialization =
|
||||||
@@ -33,9 +33,8 @@ void NonMemMovableTemplateArgChecker::check(
|
|||||||
for (unsigned i = 0; i < Args.size(); ++i) {
|
for (unsigned i = 0; i < Args.size(); ++i) {
|
||||||
QualType ArgType = Args[i].getAsType();
|
QualType ArgType = Args[i].getAsType();
|
||||||
if (NonMemMovable.hasEffectiveAnnotation(ArgType)) {
|
if (NonMemMovable.hasEffectiveAnnotation(ArgType)) {
|
||||||
diag(Specialization->getLocation(), Error,
|
diag(Specialization->getLocation(), Error, DiagnosticIDs::Error)
|
||||||
DiagnosticIDs::Error) << Specialization
|
<< Specialization << ArgType;
|
||||||
<< ArgType;
|
|
||||||
// XXX It would be really nice if we could get the instantiation stack
|
// XXX It would be really nice if we could get the instantiation stack
|
||||||
// information
|
// information
|
||||||
// from Sema such that we could print a full template instantiation stack,
|
// from Sema such that we could print a full template instantiation stack,
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ class NonMemMovableTemplateArgChecker : public BaseCheck {
|
|||||||
public:
|
public:
|
||||||
NonMemMovableTemplateArgChecker(StringRef CheckName,
|
NonMemMovableTemplateArgChecker(StringRef CheckName,
|
||||||
ContextType *Context = nullptr)
|
ContextType *Context = nullptr)
|
||||||
: BaseCheck(CheckName, Context) {}
|
: BaseCheck(CheckName, Context) {}
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,9 @@
|
|||||||
#include "NonParamInsideFunctionDeclChecker.h"
|
#include "NonParamInsideFunctionDeclChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
class NonParamAnnotation : public CustomTypeAnnotation
|
class NonParamAnnotation : public CustomTypeAnnotation {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
NonParamAnnotation() : CustomTypeAnnotation("moz_non_param", "non-param") {};
|
NonParamAnnotation() : CustomTypeAnnotation("moz_non_param", "non-param"){};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Adding alignas(_) on a struct implicitly marks it as MOZ_NON_PARAM, due to
|
// Adding alignas(_) on a struct implicitly marks it as MOZ_NON_PARAM, due to
|
||||||
@@ -28,7 +27,9 @@ protected:
|
|||||||
for (auto F : RD->fields()) {
|
for (auto F : RD->fields()) {
|
||||||
for (auto A : F->attrs()) {
|
for (auto A : F->attrs()) {
|
||||||
if (isa<AlignedAttr>(A)) {
|
if (isa<AlignedAttr>(A)) {
|
||||||
return ("member '" + F->getName() + "' has an alignas(_) annotation").str();
|
return ("member '" + F->getName() +
|
||||||
|
"' has an alignas(_) annotation")
|
||||||
|
.str();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,22 +42,22 @@ protected:
|
|||||||
};
|
};
|
||||||
NonParamAnnotation NonParam;
|
NonParamAnnotation NonParam;
|
||||||
|
|
||||||
void NonParamInsideFunctionDeclChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void NonParamInsideFunctionDeclChecker::registerMatchers(
|
||||||
|
MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(
|
AstMatcher->addMatcher(
|
||||||
functionDecl(anyOf(allOf(isDefinition(),
|
functionDecl(
|
||||||
hasAncestor(classTemplateSpecializationDecl()
|
anyOf(allOf(isDefinition(),
|
||||||
.bind("spec"))),
|
hasAncestor(
|
||||||
isDefinition()))
|
classTemplateSpecializationDecl().bind("spec"))),
|
||||||
|
isDefinition()))
|
||||||
.bind("func"),
|
.bind("func"),
|
||||||
this);
|
this);
|
||||||
AstMatcher->addMatcher(
|
AstMatcher->addMatcher(lambdaExpr().bind("lambda"), this);
|
||||||
lambdaExpr().bind("lambda"),
|
|
||||||
this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NonParamInsideFunctionDeclChecker::check(
|
void NonParamInsideFunctionDeclChecker::check(
|
||||||
const MatchFinder::MatchResult &Result) {
|
const MatchFinder::MatchResult &Result) {
|
||||||
static DenseSet<const FunctionDecl*> CheckedFunctionDecls;
|
static DenseSet<const FunctionDecl *> CheckedFunctionDecls;
|
||||||
|
|
||||||
const FunctionDecl *func = Result.Nodes.getNodeAs<FunctionDecl>("func");
|
const FunctionDecl *func = Result.Nodes.getNodeAs<FunctionDecl>("func");
|
||||||
if (!func) {
|
if (!func) {
|
||||||
@@ -98,15 +99,16 @@ void NonParamInsideFunctionDeclChecker::check(
|
|||||||
QualType T = p->getType().withoutLocalFastQualifiers();
|
QualType T = p->getType().withoutLocalFastQualifiers();
|
||||||
if (NonParam.hasEffectiveAnnotation(T)) {
|
if (NonParam.hasEffectiveAnnotation(T)) {
|
||||||
diag(p->getLocation(), "Type %0 must not be used as parameter",
|
diag(p->getLocation(), "Type %0 must not be used as parameter",
|
||||||
DiagnosticIDs::Error) << T;
|
DiagnosticIDs::Error)
|
||||||
diag(p->getLocation(), "Please consider passing a const reference instead",
|
<< T;
|
||||||
|
diag(p->getLocation(),
|
||||||
|
"Please consider passing a const reference instead",
|
||||||
DiagnosticIDs::Note);
|
DiagnosticIDs::Note);
|
||||||
|
|
||||||
if (Spec) {
|
if (Spec) {
|
||||||
diag(Spec->getPointOfInstantiation(),
|
diag(Spec->getPointOfInstantiation(),
|
||||||
"The bad argument was passed to %0 here",
|
"The bad argument was passed to %0 here", DiagnosticIDs::Note)
|
||||||
DiagnosticIDs::Note)
|
<< Spec->getSpecializedTemplate();
|
||||||
<< Spec->getSpecializedTemplate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NonParam.dumpAnnotationReason(*this, T, p->getLocation());
|
NonParam.dumpAnnotationReason(*this, T, p->getLocation());
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ class NonParamInsideFunctionDeclChecker : public BaseCheck {
|
|||||||
public:
|
public:
|
||||||
NonParamInsideFunctionDeclChecker(StringRef CheckName,
|
NonParamInsideFunctionDeclChecker(StringRef CheckName,
|
||||||
ContextType *Context = nullptr)
|
ContextType *Context = nullptr)
|
||||||
: BaseCheck(CheckName, Context) {}
|
: BaseCheck(CheckName, Context) {}
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,9 +5,8 @@
|
|||||||
#include "OverrideBaseCallChecker.h"
|
#include "OverrideBaseCallChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void OverrideBaseCallChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void OverrideBaseCallChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(cxxRecordDecl(hasBaseClasses()).bind("class"),
|
AstMatcher->addMatcher(cxxRecordDecl(hasBaseClasses()).bind("class"), this);
|
||||||
this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OverrideBaseCallChecker::isRequiredBaseMethod(
|
bool OverrideBaseCallChecker::isRequiredBaseMethod(
|
||||||
@@ -16,15 +15,15 @@ bool OverrideBaseCallChecker::isRequiredBaseMethod(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void OverrideBaseCallChecker::evaluateExpression(
|
void OverrideBaseCallChecker::evaluateExpression(
|
||||||
const Stmt *StmtExpr, std::list<const CXXMethodDecl*> &MethodList) {
|
const Stmt *StmtExpr, std::list<const CXXMethodDecl *> &MethodList) {
|
||||||
// Continue while we have methods in our list
|
// Continue while we have methods in our list
|
||||||
if (!MethodList.size()) {
|
if (!MethodList.size()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (auto MemberFuncCall = dyn_cast<CXXMemberCallExpr>(StmtExpr)) {
|
if (auto MemberFuncCall = dyn_cast<CXXMemberCallExpr>(StmtExpr)) {
|
||||||
if (auto Method = dyn_cast<CXXMethodDecl>(
|
if (auto Method =
|
||||||
MemberFuncCall->getDirectCallee())) {
|
dyn_cast<CXXMethodDecl>(MemberFuncCall->getDirectCallee())) {
|
||||||
findBaseMethodCall(Method, MethodList);
|
findBaseMethodCall(Method, MethodList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,34 +37,33 @@ void OverrideBaseCallChecker::evaluateExpression(
|
|||||||
|
|
||||||
void OverrideBaseCallChecker::getRequiredBaseMethod(
|
void OverrideBaseCallChecker::getRequiredBaseMethod(
|
||||||
const CXXMethodDecl *Method,
|
const CXXMethodDecl *Method,
|
||||||
std::list<const CXXMethodDecl*>& MethodsList) {
|
std::list<const CXXMethodDecl *> &MethodsList) {
|
||||||
|
|
||||||
if (isRequiredBaseMethod(Method)) {
|
if (isRequiredBaseMethod(Method)) {
|
||||||
MethodsList.push_back(Method);
|
MethodsList.push_back(Method);
|
||||||
} else {
|
} else {
|
||||||
// Loop through all it's base methods.
|
// Loop through all it's base methods.
|
||||||
for (auto BaseMethod = Method->begin_overridden_methods();
|
for (auto BaseMethod = Method->begin_overridden_methods();
|
||||||
BaseMethod != Method->end_overridden_methods(); BaseMethod++) {
|
BaseMethod != Method->end_overridden_methods(); BaseMethod++) {
|
||||||
getRequiredBaseMethod(*BaseMethod, MethodsList);
|
getRequiredBaseMethod(*BaseMethod, MethodsList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OverrideBaseCallChecker::findBaseMethodCall(
|
void OverrideBaseCallChecker::findBaseMethodCall(
|
||||||
const CXXMethodDecl* Method,
|
const CXXMethodDecl *Method,
|
||||||
std::list<const CXXMethodDecl*>& MethodsList) {
|
std::list<const CXXMethodDecl *> &MethodsList) {
|
||||||
|
|
||||||
MethodsList.remove(Method);
|
MethodsList.remove(Method);
|
||||||
// Loop also through all it's base methods;
|
// Loop also through all it's base methods;
|
||||||
for (auto BaseMethod = Method->begin_overridden_methods();
|
for (auto BaseMethod = Method->begin_overridden_methods();
|
||||||
BaseMethod != Method->end_overridden_methods(); BaseMethod++) {
|
BaseMethod != Method->end_overridden_methods(); BaseMethod++) {
|
||||||
findBaseMethodCall(*BaseMethod, MethodsList);
|
findBaseMethodCall(*BaseMethod, MethodsList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OverrideBaseCallChecker::check(
|
void OverrideBaseCallChecker::check(const MatchFinder::MatchResult &Result) {
|
||||||
const MatchFinder::MatchResult &Result) {
|
const char *Error =
|
||||||
const char* Error =
|
|
||||||
"Method %0 must be called in all overrides, but is not called in "
|
"Method %0 must be called in all overrides, but is not called in "
|
||||||
"this override defined for class %1";
|
"this override defined for class %1";
|
||||||
const CXXRecordDecl *Decl = Result.Nodes.getNodeAs<CXXRecordDecl>("class");
|
const CXXRecordDecl *Decl = Result.Nodes.getNodeAs<CXXRecordDecl>("class");
|
||||||
@@ -80,11 +78,11 @@ void OverrideBaseCallChecker::check(
|
|||||||
|
|
||||||
// Preferred the usage of list instead of vector in order to avoid
|
// Preferred the usage of list instead of vector in order to avoid
|
||||||
// calling erase-remove when deleting items
|
// calling erase-remove when deleting items
|
||||||
std::list<const CXXMethodDecl*> MethodsList;
|
std::list<const CXXMethodDecl *> MethodsList;
|
||||||
// For each overridden method push it to a list if it meets our
|
// For each overridden method push it to a list if it meets our
|
||||||
// criteria
|
// criteria
|
||||||
for (auto BaseMethod = Method->begin_overridden_methods();
|
for (auto BaseMethod = Method->begin_overridden_methods();
|
||||||
BaseMethod != Method->end_overridden_methods(); BaseMethod++) {
|
BaseMethod != Method->end_overridden_methods(); BaseMethod++) {
|
||||||
getRequiredBaseMethod(*BaseMethod, MethodsList);
|
getRequiredBaseMethod(*BaseMethod, MethodsList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,8 +103,7 @@ void OverrideBaseCallChecker::check(
|
|||||||
BaseMethod->printQualifiedName(OS);
|
BaseMethod->printQualifiedName(OS);
|
||||||
|
|
||||||
diag(Method->getLocation(), Error, DiagnosticIDs::Error)
|
diag(Method->getLocation(), Error, DiagnosticIDs::Error)
|
||||||
<< OS.str()
|
<< OS.str() << Decl->getName();
|
||||||
<< Decl->getName();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,18 +9,18 @@
|
|||||||
|
|
||||||
class OverrideBaseCallChecker : public BaseCheck {
|
class OverrideBaseCallChecker : public BaseCheck {
|
||||||
public:
|
public:
|
||||||
OverrideBaseCallChecker(StringRef CheckName,
|
OverrideBaseCallChecker(StringRef CheckName, ContextType *Context = nullptr)
|
||||||
ContextType *Context = nullptr)
|
: BaseCheck(CheckName, Context) {}
|
||||||
: BaseCheck(CheckName, Context) {}
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void evaluateExpression(const Stmt *StmtExpr,
|
void evaluateExpression(const Stmt *StmtExpr,
|
||||||
std::list<const CXXMethodDecl*> &MethodList);
|
std::list<const CXXMethodDecl *> &MethodList);
|
||||||
void getRequiredBaseMethod(const CXXMethodDecl* Method,
|
void getRequiredBaseMethod(const CXXMethodDecl *Method,
|
||||||
std::list<const CXXMethodDecl*>& MethodsList);
|
std::list<const CXXMethodDecl *> &MethodsList);
|
||||||
void findBaseMethodCall(const CXXMethodDecl* Method,
|
void findBaseMethodCall(const CXXMethodDecl *Method,
|
||||||
std::list<const CXXMethodDecl*>& MethodsList);
|
std::list<const CXXMethodDecl *> &MethodsList);
|
||||||
bool isRequiredBaseMethod(const CXXMethodDecl *Method);
|
bool isRequiredBaseMethod(const CXXMethodDecl *Method);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include "OverrideBaseCallUsageChecker.h"
|
#include "OverrideBaseCallUsageChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void OverrideBaseCallUsageChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void OverrideBaseCallUsageChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(
|
AstMatcher->addMatcher(
|
||||||
cxxMethodDecl(isNonVirtual(), isRequiredBaseMethod()).bind("method"),
|
cxxMethodDecl(isNonVirtual(), isRequiredBaseMethod()).bind("method"),
|
||||||
this);
|
this);
|
||||||
@@ -13,7 +13,7 @@ void OverrideBaseCallUsageChecker::registerMatchers(MatchFinder* AstMatcher) {
|
|||||||
|
|
||||||
void OverrideBaseCallUsageChecker::check(
|
void OverrideBaseCallUsageChecker::check(
|
||||||
const MatchFinder::MatchResult &Result) {
|
const MatchFinder::MatchResult &Result) {
|
||||||
const char* Error =
|
const char *Error =
|
||||||
"MOZ_REQUIRED_BASE_METHOD can be used only on virtual methods";
|
"MOZ_REQUIRED_BASE_METHOD can be used only on virtual methods";
|
||||||
const CXXMethodDecl *Method = Result.Nodes.getNodeAs<CXXMethodDecl>("method");
|
const CXXMethodDecl *Method = Result.Nodes.getNodeAs<CXXMethodDecl>("method");
|
||||||
|
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ class OverrideBaseCallUsageChecker : public BaseCheck {
|
|||||||
public:
|
public:
|
||||||
OverrideBaseCallUsageChecker(StringRef CheckName = "override-base-call-usage",
|
OverrideBaseCallUsageChecker(StringRef CheckName = "override-base-call-usage",
|
||||||
ContextType *Context = nullptr)
|
ContextType *Context = nullptr)
|
||||||
: BaseCheck(CheckName, Context) {}
|
: BaseCheck(CheckName, Context) {}
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -17,19 +17,18 @@
|
|||||||
// The RecurseGuard object will unregister its object when it is destroyed, and
|
// The RecurseGuard object will unregister its object when it is destroyed, and
|
||||||
// has a method `isRepeat()` which will return `true` if the item was already
|
// has a method `isRepeat()` which will return `true` if the item was already
|
||||||
// seen.
|
// seen.
|
||||||
template<typename T>
|
template <typename T> class RecurseGuard {
|
||||||
class RecurseGuard {
|
|
||||||
public:
|
public:
|
||||||
RecurseGuard(T Thing) : Thing(Thing), Set(new DenseSet<T>()), Repeat(false) {
|
RecurseGuard(T Thing) : Thing(Thing), Set(new DenseSet<T>()), Repeat(false) {
|
||||||
Set->insert(Thing);
|
Set->insert(Thing);
|
||||||
}
|
}
|
||||||
RecurseGuard(T Thing, std::shared_ptr<DenseSet<T>>& Set)
|
RecurseGuard(T Thing, std::shared_ptr<DenseSet<T>> &Set)
|
||||||
: Thing(Thing), Set(Set), Repeat(false) {
|
: Thing(Thing), Set(Set), Repeat(false) {
|
||||||
Repeat = !Set->insert(Thing).second;
|
Repeat = !Set->insert(Thing).second;
|
||||||
}
|
}
|
||||||
RecurseGuard(const RecurseGuard &) = delete;
|
RecurseGuard(const RecurseGuard &) = delete;
|
||||||
RecurseGuard(RecurseGuard && Other)
|
RecurseGuard(RecurseGuard &&Other)
|
||||||
: Thing(Other.Thing), Set(Other.Set), Repeat(Other.Repeat) {
|
: Thing(Other.Thing), Set(Other.Set), Repeat(Other.Repeat) {
|
||||||
Other.Repeat = true;
|
Other.Repeat = true;
|
||||||
}
|
}
|
||||||
~RecurseGuard() {
|
~RecurseGuard() {
|
||||||
@@ -42,17 +41,11 @@ public:
|
|||||||
|
|
||||||
T get() { return Thing; }
|
T get() { return Thing; }
|
||||||
|
|
||||||
operator T() {
|
operator T() { return Thing; }
|
||||||
return Thing;
|
|
||||||
}
|
|
||||||
|
|
||||||
T operator ->() {
|
T operator->() { return Thing; }
|
||||||
return Thing;
|
|
||||||
}
|
|
||||||
|
|
||||||
RecurseGuard recurse(T NewThing) {
|
RecurseGuard recurse(T NewThing) { return RecurseGuard(NewThing, Set); }
|
||||||
return RecurseGuard(NewThing, Set);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T Thing;
|
T Thing;
|
||||||
|
|||||||
@@ -5,7 +5,8 @@
|
|||||||
#include "RefCountedCopyConstructorChecker.h"
|
#include "RefCountedCopyConstructorChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void RefCountedCopyConstructorChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void RefCountedCopyConstructorChecker::registerMatchers(
|
||||||
|
MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(
|
AstMatcher->addMatcher(
|
||||||
cxxConstructExpr(
|
cxxConstructExpr(
|
||||||
hasDeclaration(cxxConstructorDecl(isCompilerProvidedCopyConstructor(),
|
hasDeclaration(cxxConstructorDecl(isCompilerProvidedCopyConstructor(),
|
||||||
@@ -16,14 +17,13 @@ void RefCountedCopyConstructorChecker::registerMatchers(MatchFinder* AstMatcher)
|
|||||||
|
|
||||||
void RefCountedCopyConstructorChecker::check(
|
void RefCountedCopyConstructorChecker::check(
|
||||||
const MatchFinder::MatchResult &Result) {
|
const MatchFinder::MatchResult &Result) {
|
||||||
const char* Error =
|
const char *Error =
|
||||||
"Invalid use of compiler-provided copy constructor on refcounted type";
|
"Invalid use of compiler-provided copy constructor on refcounted type";
|
||||||
const char* Note =
|
const char *Note = "The default copy constructor also copies the "
|
||||||
"The default copy constructor also copies the "
|
"default mRefCnt property, leading to reference "
|
||||||
"default mRefCnt property, leading to reference "
|
"count imbalance issues. Please provide your own "
|
||||||
"count imbalance issues. Please provide your own "
|
"copy constructor which only copies the fields which "
|
||||||
"copy constructor which only copies the fields which "
|
"need to be copied";
|
||||||
"need to be copied";
|
|
||||||
|
|
||||||
// Everything we needed to know was checked in the matcher - we just report
|
// Everything we needed to know was checked in the matcher - we just report
|
||||||
// the error here
|
// the error here
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ class RefCountedCopyConstructorChecker : public BaseCheck {
|
|||||||
public:
|
public:
|
||||||
RefCountedCopyConstructorChecker(StringRef CheckName,
|
RefCountedCopyConstructorChecker(StringRef CheckName,
|
||||||
ContextType *Context = nullptr)
|
ContextType *Context = nullptr)
|
||||||
: BaseCheck(CheckName, Context) {}
|
: BaseCheck(CheckName, Context) {}
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
RefCountedMap RefCountedClasses;
|
RefCountedMap RefCountedClasses;
|
||||||
|
|
||||||
void RefCountedInsideLambdaChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void RefCountedInsideLambdaChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
// We want to reject any code which captures a pointer to an object of a
|
// We want to reject any code which captures a pointer to an object of a
|
||||||
// refcounted type, and then lets that value escape. As a primitive analysis,
|
// refcounted type, and then lets that value escape. As a primitive analysis,
|
||||||
// we reject any occurances of the lambda as a template parameter to a class
|
// we reject any occurances of the lambda as a template parameter to a class
|
||||||
@@ -15,36 +15,35 @@ void RefCountedInsideLambdaChecker::registerMatchers(MatchFinder* AstMatcher) {
|
|||||||
// in a return value (either from lambdas, or in c++14, auto functions).
|
// in a return value (either from lambdas, or in c++14, auto functions).
|
||||||
//
|
//
|
||||||
// We check these lambdas' capture lists for raw pointers to refcounted types.
|
// We check these lambdas' capture lists for raw pointers to refcounted types.
|
||||||
|
AstMatcher->addMatcher(functionDecl(returns(recordType(hasDeclaration(
|
||||||
|
cxxRecordDecl(isLambdaDecl()).bind("decl"))))),
|
||||||
|
this);
|
||||||
|
AstMatcher->addMatcher(lambdaExpr().bind("lambdaExpr"), this);
|
||||||
AstMatcher->addMatcher(
|
AstMatcher->addMatcher(
|
||||||
functionDecl(returns(recordType(hasDeclaration(cxxRecordDecl(
|
classTemplateSpecializationDecl(
|
||||||
isLambdaDecl()).bind("decl"))))),
|
hasAnyTemplateArgument(refersToType(recordType(
|
||||||
this);
|
hasDeclaration(cxxRecordDecl(isLambdaDecl()).bind("decl")))))),
|
||||||
AstMatcher->addMatcher(lambdaExpr().bind("lambdaExpr"),
|
|
||||||
this);
|
|
||||||
AstMatcher->addMatcher(
|
|
||||||
classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
|
|
||||||
recordType(hasDeclaration(cxxRecordDecl(
|
|
||||||
isLambdaDecl()).bind("decl")))))),
|
|
||||||
this);
|
this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RefCountedInsideLambdaChecker::emitDiagnostics(SourceLocation Loc,
|
void RefCountedInsideLambdaChecker::emitDiagnostics(SourceLocation Loc,
|
||||||
StringRef Name,
|
StringRef Name,
|
||||||
QualType Type) {
|
QualType Type) {
|
||||||
diag(Loc, "Refcounted variable '%0' of type %1 cannot be captured by a lambda",
|
diag(Loc,
|
||||||
DiagnosticIDs::Error) << Name << Type;
|
"Refcounted variable '%0' of type %1 cannot be captured by a lambda",
|
||||||
diag(Loc, "Please consider using a smart pointer",
|
DiagnosticIDs::Error)
|
||||||
DiagnosticIDs::Note);
|
<< Name << Type;
|
||||||
|
diag(Loc, "Please consider using a smart pointer", DiagnosticIDs::Note);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RefCountedInsideLambdaChecker::check(
|
void RefCountedInsideLambdaChecker::check(
|
||||||
const MatchFinder::MatchResult &Result) {
|
const MatchFinder::MatchResult &Result) {
|
||||||
static DenseSet<const CXXRecordDecl*> CheckedDecls;
|
static DenseSet<const CXXRecordDecl *> CheckedDecls;
|
||||||
|
|
||||||
const CXXRecordDecl *Lambda = Result.Nodes.getNodeAs<CXXRecordDecl>("decl");
|
const CXXRecordDecl *Lambda = Result.Nodes.getNodeAs<CXXRecordDecl>("decl");
|
||||||
|
|
||||||
if (const LambdaExpr *OuterLambda =
|
if (const LambdaExpr *OuterLambda =
|
||||||
Result.Nodes.getNodeAs<LambdaExpr>("lambdaExpr")) {
|
Result.Nodes.getNodeAs<LambdaExpr>("lambdaExpr")) {
|
||||||
const CXXMethodDecl *OpCall = OuterLambda->getCallOperator();
|
const CXXMethodDecl *OpCall = OuterLambda->getCallOperator();
|
||||||
QualType ReturnTy = OpCall->getReturnType();
|
QualType ReturnTy = OpCall->getReturnType();
|
||||||
if (const CXXRecordDecl *Record = ReturnTy->getAsCXXRecordDecl()) {
|
if (const CXXRecordDecl *Record = ReturnTy->getAsCXXRecordDecl()) {
|
||||||
@@ -64,19 +63,20 @@ void RefCountedInsideLambdaChecker::check(
|
|||||||
|
|
||||||
bool StrongRefToThisCaptured = false;
|
bool StrongRefToThisCaptured = false;
|
||||||
|
|
||||||
for (const LambdaCapture& Capture : Lambda->captures()) {
|
for (const LambdaCapture &Capture : Lambda->captures()) {
|
||||||
// Check if any of the captures are ByRef. If they are, we have nothing to
|
// Check if any of the captures are ByRef. If they are, we have nothing to
|
||||||
// report, as it's OK to capture raw pointers to refcounted objects so long as
|
// report, as it's OK to capture raw pointers to refcounted objects so long
|
||||||
// the Lambda doesn't escape the current scope, which is required by ByRef
|
// as the Lambda doesn't escape the current scope, which is required by
|
||||||
// captures already.
|
// ByRef captures already.
|
||||||
if (Capture.getCaptureKind() == LCK_ByRef) {
|
if (Capture.getCaptureKind() == LCK_ByRef) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if this capture is byvalue, and captures a strong reference to this.
|
// Check if this capture is byvalue, and captures a strong reference to
|
||||||
// XXX: Do we want to make sure that this type which we are capturing is a "Smart Pointer" somehow?
|
// this.
|
||||||
if (!StrongRefToThisCaptured &&
|
// XXX: Do we want to make sure that this type which we are capturing is a
|
||||||
Capture.capturesVariable() &&
|
// "Smart Pointer" somehow?
|
||||||
|
if (!StrongRefToThisCaptured && Capture.capturesVariable() &&
|
||||||
Capture.getCaptureKind() == LCK_ByCopy) {
|
Capture.getCaptureKind() == LCK_ByCopy) {
|
||||||
const VarDecl *Var = Capture.getCapturedVar();
|
const VarDecl *Var = Capture.getCapturedVar();
|
||||||
if (Var->hasInit()) {
|
if (Var->hasInit()) {
|
||||||
@@ -103,13 +103,15 @@ void RefCountedInsideLambdaChecker::check(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now we can go through and produce errors for any captured variables or this pointers.
|
// Now we can go through and produce errors for any captured variables or this
|
||||||
for (const LambdaCapture& Capture : Lambda->captures()) {
|
// pointers.
|
||||||
|
for (const LambdaCapture &Capture : Lambda->captures()) {
|
||||||
if (Capture.capturesVariable()) {
|
if (Capture.capturesVariable()) {
|
||||||
QualType Pointee = Capture.getCapturedVar()->getType()->getPointeeType();
|
QualType Pointee = Capture.getCapturedVar()->getType()->getPointeeType();
|
||||||
|
|
||||||
if (!Pointee.isNull() && isClassRefCounted(Pointee)) {
|
if (!Pointee.isNull() && isClassRefCounted(Pointee)) {
|
||||||
emitDiagnostics(Capture.getLocation(), Capture.getCapturedVar()->getName(), Pointee);
|
emitDiagnostics(Capture.getLocation(),
|
||||||
|
Capture.getCapturedVar()->getName(), Pointee);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,12 +127,12 @@ void RefCountedInsideLambdaChecker::check(
|
|||||||
// captured implicitly when the LambdaCaptureDefault was LCD_ByRef, as that
|
// captured implicitly when the LambdaCaptureDefault was LCD_ByRef, as that
|
||||||
// expresses the intent that the lambda won't leave the enclosing scope.
|
// expresses the intent that the lambda won't leave the enclosing scope.
|
||||||
bool ImplicitByRefDefaultedCapture =
|
bool ImplicitByRefDefaultedCapture =
|
||||||
Capture.isImplicit() && Lambda->getLambdaCaptureDefault() == LCD_ByRef;
|
Capture.isImplicit() && Lambda->getLambdaCaptureDefault() == LCD_ByRef;
|
||||||
if (Capture.capturesThis() &&
|
if (Capture.capturesThis() && !ImplicitByRefDefaultedCapture &&
|
||||||
!ImplicitByRefDefaultedCapture &&
|
|
||||||
!StrongRefToThisCaptured) {
|
!StrongRefToThisCaptured) {
|
||||||
ThisVisitor V(*this);
|
ThisVisitor V(*this);
|
||||||
bool NotAborted = V.TraverseDecl(const_cast<CXXMethodDecl *>(Lambda->getLambdaCallOperator()));
|
bool NotAborted = V.TraverseDecl(
|
||||||
|
const_cast<CXXMethodDecl *>(Lambda->getLambdaCallOperator()));
|
||||||
if (!NotAborted) {
|
if (!NotAborted) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -138,7 +140,8 @@ void RefCountedInsideLambdaChecker::check(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RefCountedInsideLambdaChecker::ThisVisitor::VisitCXXThisExpr(CXXThisExpr *This) {
|
bool RefCountedInsideLambdaChecker::ThisVisitor::VisitCXXThisExpr(
|
||||||
|
CXXThisExpr *This) {
|
||||||
QualType Pointee = This->getType()->getPointeeType();
|
QualType Pointee = This->getType()->getPointeeType();
|
||||||
if (!Pointee.isNull() && isClassRefCounted(Pointee)) {
|
if (!Pointee.isNull() && isClassRefCounted(Pointee)) {
|
||||||
Checker.emitDiagnostics(This->getLocStart(), "this", Pointee);
|
Checker.emitDiagnostics(This->getLocStart(), "this", Pointee);
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ class RefCountedInsideLambdaChecker : public BaseCheck {
|
|||||||
public:
|
public:
|
||||||
RefCountedInsideLambdaChecker(StringRef CheckName,
|
RefCountedInsideLambdaChecker(StringRef CheckName,
|
||||||
ContextType *Context = nullptr)
|
ContextType *Context = nullptr)
|
||||||
: BaseCheck(CheckName, Context) {}
|
: BaseCheck(CheckName, Context) {}
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
|
|
||||||
void emitDiagnostics(SourceLocation Loc, StringRef Name, QualType Type);
|
void emitDiagnostics(SourceLocation Loc, StringRef Name, QualType Type);
|
||||||
@@ -20,12 +20,13 @@ public:
|
|||||||
private:
|
private:
|
||||||
class ThisVisitor : public RecursiveASTVisitor<ThisVisitor> {
|
class ThisVisitor : public RecursiveASTVisitor<ThisVisitor> {
|
||||||
public:
|
public:
|
||||||
explicit ThisVisitor(RefCountedInsideLambdaChecker& Checker)
|
explicit ThisVisitor(RefCountedInsideLambdaChecker &Checker)
|
||||||
: Checker(Checker) {}
|
: Checker(Checker) {}
|
||||||
|
|
||||||
bool VisitCXXThisExpr(CXXThisExpr *This);
|
bool VisitCXXThisExpr(CXXThisExpr *This);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RefCountedInsideLambdaChecker& Checker;
|
RefCountedInsideLambdaChecker &Checker;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,13 +5,12 @@
|
|||||||
#include "ScopeChecker.h"
|
#include "ScopeChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void ScopeChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void ScopeChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(varDecl().bind("node"), this);
|
AstMatcher->addMatcher(varDecl().bind("node"), this);
|
||||||
AstMatcher->addMatcher(cxxNewExpr().bind("node"), this);
|
AstMatcher->addMatcher(cxxNewExpr().bind("node"), this);
|
||||||
AstMatcher->addMatcher(materializeTemporaryExpr().bind("node"), this);
|
AstMatcher->addMatcher(materializeTemporaryExpr().bind("node"), this);
|
||||||
AstMatcher->addMatcher(
|
AstMatcher->addMatcher(
|
||||||
callExpr(callee(functionDecl(heapAllocator()))).bind("node"),
|
callExpr(callee(functionDecl(heapAllocator()))).bind("node"), this);
|
||||||
this);
|
|
||||||
AstMatcher->addMatcher(parmVarDecl().bind("parm_vardecl"), this);
|
AstMatcher->addMatcher(parmVarDecl().bind("parm_vardecl"), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,8 +30,7 @@ typedef DenseMap<const MaterializeTemporaryExpr *, const Decl *>
|
|||||||
AutomaticTemporaryMap;
|
AutomaticTemporaryMap;
|
||||||
AutomaticTemporaryMap AutomaticTemporaries;
|
AutomaticTemporaryMap AutomaticTemporaries;
|
||||||
|
|
||||||
void ScopeChecker::check(
|
void ScopeChecker::check(const MatchFinder::MatchResult &Result) {
|
||||||
const MatchFinder::MatchResult &Result) {
|
|
||||||
// There are a variety of different reasons why something could be allocated
|
// There are a variety of different reasons why something could be allocated
|
||||||
AllocationVariety Variety = AV_None;
|
AllocationVariety Variety = AV_None;
|
||||||
SourceLocation Loc;
|
SourceLocation Loc;
|
||||||
@@ -120,25 +118,17 @@ void ScopeChecker::check(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Error messages for incorrect allocations.
|
// Error messages for incorrect allocations.
|
||||||
const char* Stack =
|
const char *Stack = "variable of type %0 only valid on the stack";
|
||||||
"variable of type %0 only valid on the stack";
|
const char *Global = "variable of type %0 only valid as global";
|
||||||
const char* Global =
|
const char *Heap = "variable of type %0 only valid on the heap";
|
||||||
"variable of type %0 only valid as global";
|
const char *NonHeap = "variable of type %0 is not valid on the heap";
|
||||||
const char* Heap =
|
const char *NonTemporary = "variable of type %0 is not valid in a temporary";
|
||||||
"variable of type %0 only valid on the heap";
|
|
||||||
const char* NonHeap =
|
|
||||||
"variable of type %0 is not valid on the heap";
|
|
||||||
const char* NonTemporary =
|
|
||||||
"variable of type %0 is not valid in a temporary";
|
|
||||||
|
|
||||||
const char* StackNote =
|
const char *StackNote =
|
||||||
"value incorrectly allocated in an automatic variable";
|
"value incorrectly allocated in an automatic variable";
|
||||||
const char* GlobalNote =
|
const char *GlobalNote = "value incorrectly allocated in a global variable";
|
||||||
"value incorrectly allocated in a global variable";
|
const char *HeapNote = "value incorrectly allocated on the heap";
|
||||||
const char* HeapNote =
|
const char *TemporaryNote = "value incorrectly allocated in a temporary";
|
||||||
"value incorrectly allocated on the heap";
|
|
||||||
const char* TemporaryNote =
|
|
||||||
"value incorrectly allocated in a temporary";
|
|
||||||
|
|
||||||
// Report errors depending on the annotations on the input types.
|
// Report errors depending on the annotations on the input types.
|
||||||
switch (Variety) {
|
switch (Variety) {
|
||||||
|
|||||||
@@ -10,8 +10,8 @@
|
|||||||
class ScopeChecker : public BaseCheck {
|
class ScopeChecker : public BaseCheck {
|
||||||
public:
|
public:
|
||||||
ScopeChecker(StringRef CheckName, ContextType *Context = nullptr)
|
ScopeChecker(StringRef CheckName, ContextType *Context = nullptr)
|
||||||
: BaseCheck(CheckName, Context) {}
|
: BaseCheck(CheckName, Context) {}
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,31 +5,34 @@
|
|||||||
#include "SprintfLiteralChecker.h"
|
#include "SprintfLiteralChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void SprintfLiteralChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void SprintfLiteralChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(
|
AstMatcher->addMatcher(
|
||||||
callExpr(isSnprintfLikeFunc(),
|
callExpr(
|
||||||
allOf(hasArgument(0, ignoringParenImpCasts(declRefExpr().bind("buffer"))),
|
isSnprintfLikeFunc(),
|
||||||
anyOf(hasArgument(1, sizeOfExpr(hasIgnoringParenImpCasts(declRefExpr().bind("size")))),
|
allOf(hasArgument(
|
||||||
hasArgument(1, integerLiteral().bind("immediate")),
|
0, ignoringParenImpCasts(declRefExpr().bind("buffer"))),
|
||||||
hasArgument(1, declRefExpr(to(varDecl(hasType(isConstQualified()),
|
anyOf(hasArgument(1, sizeOfExpr(hasIgnoringParenImpCasts(
|
||||||
hasInitializer(integerLiteral().bind("constant")))))))))
|
declRefExpr().bind("size")))),
|
||||||
.bind("funcCall"),
|
hasArgument(1, integerLiteral().bind("immediate")),
|
||||||
this
|
hasArgument(1, declRefExpr(to(varDecl(
|
||||||
);
|
hasType(isConstQualified()),
|
||||||
|
hasInitializer(integerLiteral().bind(
|
||||||
|
"constant")))))))))
|
||||||
|
.bind("funcCall"),
|
||||||
|
this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SprintfLiteralChecker::check(
|
void SprintfLiteralChecker::check(const MatchFinder::MatchResult &Result) {
|
||||||
const MatchFinder::MatchResult &Result) {
|
|
||||||
if (!Result.Context->getLangOpts().CPlusPlus) {
|
if (!Result.Context->getLangOpts().CPlusPlus) {
|
||||||
// SprintfLiteral is not usable in C, so there is no point in issuing these
|
// SprintfLiteral is not usable in C, so there is no point in issuing these
|
||||||
// warnings.
|
// warnings.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* Error =
|
const char *Error =
|
||||||
"Use %1 instead of %0 when writing into a character array.";
|
"Use %1 instead of %0 when writing into a character array.";
|
||||||
const char* Note =
|
const char *Note =
|
||||||
"This will prevent passing in the wrong size to %0 accidentally.";
|
"This will prevent passing in the wrong size to %0 accidentally.";
|
||||||
|
|
||||||
const CallExpr *D = Result.Nodes.getNodeAs<CallExpr>("funcCall");
|
const CallExpr *D = Result.Nodes.getNodeAs<CallExpr>("funcCall");
|
||||||
|
|
||||||
@@ -56,21 +59,25 @@ void SprintfLiteralChecker::check(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const QualType QType = Buffer->getType();
|
const QualType QType = Buffer->getType();
|
||||||
const ConstantArrayType *Type = dyn_cast<ConstantArrayType>(QType.getTypePtrOrNull());
|
const ConstantArrayType *Type =
|
||||||
|
dyn_cast<ConstantArrayType>(QType.getTypePtrOrNull());
|
||||||
if (Type) {
|
if (Type) {
|
||||||
// Match calls like snprintf(x, 100, ...), where x is int[100];
|
// Match calls like snprintf(x, 100, ...), where x is int[100];
|
||||||
const IntegerLiteral *Literal = Result.Nodes.getNodeAs<IntegerLiteral>("immediate");
|
const IntegerLiteral *Literal =
|
||||||
|
Result.Nodes.getNodeAs<IntegerLiteral>("immediate");
|
||||||
if (!Literal) {
|
if (!Literal) {
|
||||||
// Match calls like: const int y = 100; snprintf(x, y, ...);
|
// Match calls like: const int y = 100; snprintf(x, y, ...);
|
||||||
Literal = Result.Nodes.getNodeAs<IntegerLiteral>("constant");
|
Literal = Result.Nodes.getNodeAs<IntegerLiteral>("constant");
|
||||||
}
|
}
|
||||||
|
|
||||||
// We're going to assume here that the bitwidth of both of these values fits within 64 bits.
|
// We're going to assume here that the bitwidth of both of these values fits
|
||||||
// and zero-extend both values to 64-bits before comparing them.
|
// within 64 bits. and zero-extend both values to 64-bits before comparing
|
||||||
|
// them.
|
||||||
uint64_t Size = Type->getSize().getZExtValue();
|
uint64_t Size = Type->getSize().getZExtValue();
|
||||||
uint64_t Lit = Literal->getValue().getZExtValue();
|
uint64_t Lit = Literal->getValue().getZExtValue();
|
||||||
if (Size <= Lit) {
|
if (Size <= Lit) {
|
||||||
diag(D->getLocStart(), Error, DiagnosticIDs::Error) << Name << Replacement;
|
diag(D->getLocStart(), Error, DiagnosticIDs::Error)
|
||||||
|
<< Name << Replacement;
|
||||||
diag(D->getLocStart(), Note, DiagnosticIDs::Note) << Name;
|
diag(D->getLocStart(), Note, DiagnosticIDs::Note) << Name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,9 @@
|
|||||||
|
|
||||||
class SprintfLiteralChecker : public BaseCheck {
|
class SprintfLiteralChecker : public BaseCheck {
|
||||||
public:
|
public:
|
||||||
SprintfLiteralChecker(StringRef CheckName,
|
SprintfLiteralChecker(StringRef CheckName, ContextType *Context = nullptr)
|
||||||
ContextType *Context = nullptr)
|
: BaseCheck(CheckName, Context) {}
|
||||||
: BaseCheck(CheckName, Context) {}
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,8 @@ inline SmallVector<const Stmt *, 1> getParentStmts(const Stmt *S,
|
|||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This class is a modified version of the class from clang-tidy's ExprSequence.cpp
|
// This class is a modified version of the class from clang-tidy's
|
||||||
|
// ExprSequence.cpp
|
||||||
//
|
//
|
||||||
// Maps `Stmt`s to the `CFGBlock` that contains them. Some `Stmt`s may be
|
// Maps `Stmt`s to the `CFGBlock` that contains them. Some `Stmt`s may be
|
||||||
// contained in more than one `CFGBlock`; in this case, they are mapped to the
|
// contained in more than one `CFGBlock`; in this case, they are mapped to the
|
||||||
@@ -48,7 +49,8 @@ inline SmallVector<const Stmt *, 1> getParentStmts(const Stmt *S,
|
|||||||
class StmtToBlockMap {
|
class StmtToBlockMap {
|
||||||
public:
|
public:
|
||||||
// Initializes the map for the given `CFG`.
|
// Initializes the map for the given `CFG`.
|
||||||
StmtToBlockMap(const CFG *TheCFG, ASTContext *TheContext) : Context(TheContext) {
|
StmtToBlockMap(const CFG *TheCFG, ASTContext *TheContext)
|
||||||
|
: Context(TheContext) {
|
||||||
for (const auto *B : *TheCFG) {
|
for (const auto *B : *TheCFG) {
|
||||||
for (size_t I = 0; I < B->size(); ++I) {
|
for (size_t I = 0; I < B->size(); ++I) {
|
||||||
if (Optional<CFGStmt> S = (*B)[I].getAs<CFGStmt>()) {
|
if (Optional<CFGStmt> S = (*B)[I].getAs<CFGStmt>()) {
|
||||||
@@ -64,7 +66,8 @@ public:
|
|||||||
//
|
//
|
||||||
// The optional outparameter `Index` is set to the index into the block where
|
// The optional outparameter `Index` is set to the index into the block where
|
||||||
// the `Stmt` was found.
|
// the `Stmt` was found.
|
||||||
const CFGBlock *blockContainingStmt(const Stmt *S, size_t *Index = nullptr) const {
|
const CFGBlock *blockContainingStmt(const Stmt *S,
|
||||||
|
size_t *Index = nullptr) const {
|
||||||
while (!Map.count(S)) {
|
while (!Map.count(S)) {
|
||||||
SmallVector<const Stmt *, 1> Parents = getParentStmts(S, Context);
|
SmallVector<const Stmt *, 1> Parents = getParentStmts(S, Context);
|
||||||
if (Parents.empty())
|
if (Parents.empty())
|
||||||
@@ -73,7 +76,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
const auto &E = Map.lookup(S);
|
const auto &E = Map.lookup(S);
|
||||||
if (Index) *Index = E.second;
|
if (Index)
|
||||||
|
*Index = E.second;
|
||||||
return E.first;
|
return E.first;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
// These two values are defined in ThirdPartyPaths.cpp, which is a file
|
// These two values are defined in ThirdPartyPaths.cpp, which is a file
|
||||||
// generated by ThirdPartyPaths.py.
|
// generated by ThirdPartyPaths.py.
|
||||||
|
|
||||||
extern const char* MOZ_THIRD_PARTY_PATHS[];
|
extern const char *MOZ_THIRD_PARTY_PATHS[];
|
||||||
|
|
||||||
extern const uint32_t MOZ_THIRD_PARTY_PATHS_COUNT;
|
extern const uint32_t MOZ_THIRD_PARTY_PATHS_COUNT;
|
||||||
|
|
||||||
|
|||||||
@@ -5,15 +5,13 @@
|
|||||||
#include "TrivialCtorDtorChecker.h"
|
#include "TrivialCtorDtorChecker.h"
|
||||||
#include "CustomMatchers.h"
|
#include "CustomMatchers.h"
|
||||||
|
|
||||||
void TrivialCtorDtorChecker::registerMatchers(MatchFinder* AstMatcher) {
|
void TrivialCtorDtorChecker::registerMatchers(MatchFinder *AstMatcher) {
|
||||||
AstMatcher->addMatcher(cxxRecordDecl(hasTrivialCtorDtor()).bind("node"),
|
AstMatcher->addMatcher(cxxRecordDecl(hasTrivialCtorDtor()).bind("node"),
|
||||||
this);
|
this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TrivialCtorDtorChecker::check(
|
void TrivialCtorDtorChecker::check(const MatchFinder::MatchResult &Result) {
|
||||||
const MatchFinder::MatchResult &Result) {
|
const char *Error = "class %0 must have trivial constructors and destructors";
|
||||||
const char* Error =
|
|
||||||
"class %0 must have trivial constructors and destructors";
|
|
||||||
const CXXRecordDecl *Node = Result.Nodes.getNodeAs<CXXRecordDecl>("node");
|
const CXXRecordDecl *Node = Result.Nodes.getNodeAs<CXXRecordDecl>("node");
|
||||||
|
|
||||||
if (!Node->hasDefinition()) {
|
if (!Node->hasDefinition()) {
|
||||||
|
|||||||
@@ -9,10 +9,9 @@
|
|||||||
|
|
||||||
class TrivialCtorDtorChecker : public BaseCheck {
|
class TrivialCtorDtorChecker : public BaseCheck {
|
||||||
public:
|
public:
|
||||||
TrivialCtorDtorChecker(StringRef CheckName,
|
TrivialCtorDtorChecker(StringRef CheckName, ContextType *Context = nullptr)
|
||||||
ContextType *Context = nullptr)
|
: BaseCheck(CheckName, Context) {}
|
||||||
: BaseCheck(CheckName, Context) {}
|
void registerMatchers(MatchFinder *AstMatcher) override;
|
||||||
void registerMatchers(MatchFinder* AstMatcher) override;
|
|
||||||
void check(const MatchFinder::MatchResult &Result) override;
|
void check(const MatchFinder::MatchResult &Result) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -428,7 +428,7 @@ inline bool inThirdPartyPath(const Decl *D, ASTContext *context) {
|
|||||||
return inThirdPartyPath(Loc, SM);
|
return inThirdPartyPath(Loc, SM);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline CXXRecordDecl* getNonTemplateSpecializedCXXRecordDecl(QualType Q) {
|
inline CXXRecordDecl *getNonTemplateSpecializedCXXRecordDecl(QualType Q) {
|
||||||
auto *D = Q->getAsCXXRecordDecl();
|
auto *D = Q->getAsCXXRecordDecl();
|
||||||
|
|
||||||
if (!D) {
|
if (!D) {
|
||||||
@@ -463,22 +463,22 @@ inline bool inThirdPartyPath(const Stmt *S, ASTContext *context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Polyfill for CXXOperatorCallExpr::isInfixBinaryOp()
|
/// Polyfill for CXXOperatorCallExpr::isInfixBinaryOp()
|
||||||
inline bool isInfixBinaryOp(const CXXOperatorCallExpr* OpCall) {
|
inline bool isInfixBinaryOp(const CXXOperatorCallExpr *OpCall) {
|
||||||
#if CLANG_VERSION_FULL >= 400
|
#if CLANG_VERSION_FULL >= 400
|
||||||
return OpCall->isInfixBinaryOp();
|
return OpCall->isInfixBinaryOp();
|
||||||
#else
|
#else
|
||||||
// Taken from clang source.
|
// Taken from clang source.
|
||||||
if (OpCall->getNumArgs() != 2)
|
if (OpCall->getNumArgs() != 2)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
switch (OpCall->getOperator()) {
|
switch (OpCall->getOperator()) {
|
||||||
case OO_Call: case OO_Subscript:
|
case OO_Call:
|
||||||
return false;
|
case OO_Subscript:
|
||||||
default:
|
return false;
|
||||||
return true;
|
default:
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,28 +1,26 @@
|
|||||||
#include "VariableUsageHelpers.h"
|
#include "VariableUsageHelpers.h"
|
||||||
#include "Utils.h"
|
#include "Utils.h"
|
||||||
|
|
||||||
std::vector<const Stmt*>
|
std::vector<const Stmt *> getUsageAsRvalue(const ValueDecl *ValueDeclaration,
|
||||||
getUsageAsRvalue(const ValueDecl* ValueDeclaration,
|
const FunctionDecl *FuncDecl) {
|
||||||
const FunctionDecl* FuncDecl) {
|
std::vector<const Stmt *> UsageStatements;
|
||||||
std::vector<const Stmt*> UsageStatements;
|
|
||||||
|
|
||||||
// We check the function declaration has a body.
|
// We check the function declaration has a body.
|
||||||
auto Body = FuncDecl->getBody();
|
auto Body = FuncDecl->getBody();
|
||||||
if (!Body) {
|
if (!Body) {
|
||||||
return std::vector<const Stmt*>();
|
return std::vector<const Stmt *>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// We build a Control Flow Graph (CFG) fron the body of the function
|
// We build a Control Flow Graph (CFG) fron the body of the function
|
||||||
// declaration.
|
// declaration.
|
||||||
std::unique_ptr<CFG> StatementCFG
|
std::unique_ptr<CFG> StatementCFG = CFG::buildCFG(
|
||||||
= CFG::buildCFG(FuncDecl, Body, &FuncDecl->getASTContext(),
|
FuncDecl, Body, &FuncDecl->getASTContext(), CFG::BuildOptions());
|
||||||
CFG::BuildOptions());
|
|
||||||
|
|
||||||
// We iterate through all the CFGBlocks, which basically means that we go over
|
// We iterate through all the CFGBlocks, which basically means that we go over
|
||||||
// all the possible branches of the code and therefore cover all statements.
|
// all the possible branches of the code and therefore cover all statements.
|
||||||
for (auto& Block : *StatementCFG) {
|
for (auto &Block : *StatementCFG) {
|
||||||
// We iterate through all the statements of the block.
|
// We iterate through all the statements of the block.
|
||||||
for (auto& BlockItem : *Block) {
|
for (auto &BlockItem : *Block) {
|
||||||
Optional<CFGStmt> CFGStatement = BlockItem.getAs<CFGStmt>();
|
Optional<CFGStmt> CFGStatement = BlockItem.getAs<CFGStmt>();
|
||||||
if (!CFGStatement) {
|
if (!CFGStatement) {
|
||||||
continue;
|
continue;
|
||||||
@@ -50,7 +48,7 @@ getUsageAsRvalue(const ValueDecl* ValueDeclaration,
|
|||||||
// We want our declaration to be used as the expression of the return
|
// We want our declaration to be used as the expression of the return
|
||||||
// statement.
|
// statement.
|
||||||
auto DeclRef = dyn_cast_or_null<DeclRefExpr>(
|
auto DeclRef = dyn_cast_or_null<DeclRefExpr>(
|
||||||
IgnoreTrivials(Return->getRetValue()));
|
IgnoreTrivials(Return->getRetValue()));
|
||||||
if (!DeclRef) {
|
if (!DeclRef) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -71,63 +69,56 @@ getUsageAsRvalue(const ValueDecl* ValueDeclaration,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// We declare our EscapesFunctionError enum to be an error code enum.
|
// We declare our EscapesFunctionError enum to be an error code enum.
|
||||||
namespace std
|
namespace std {
|
||||||
{
|
template <> struct is_error_code_enum<EscapesFunctionError> : true_type {};
|
||||||
template <>
|
} // namespace std
|
||||||
struct is_error_code_enum<EscapesFunctionError> : true_type {};
|
|
||||||
}
|
|
||||||
|
|
||||||
// We define the EscapesFunctionErrorCategory which contains the error messages
|
// We define the EscapesFunctionErrorCategory which contains the error messages
|
||||||
// corresponding to each enum variant.
|
// corresponding to each enum variant.
|
||||||
namespace {
|
namespace {
|
||||||
struct EscapesFunctionErrorCategory : std::error_category
|
struct EscapesFunctionErrorCategory : std::error_category {
|
||||||
{
|
const char *name() const noexcept override;
|
||||||
const char* name() const noexcept override;
|
std::string message(int ev) const override;
|
||||||
std::string message(int ev) const override;
|
};
|
||||||
};
|
|
||||||
|
|
||||||
const char* EscapesFunctionErrorCategory::name() const noexcept
|
const char *EscapesFunctionErrorCategory::name() const noexcept {
|
||||||
{
|
return "escapes function";
|
||||||
return "escapes function";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string EscapesFunctionErrorCategory::message(int ev) const
|
|
||||||
{
|
|
||||||
switch (static_cast<EscapesFunctionError>(ev))
|
|
||||||
{
|
|
||||||
case EscapesFunctionError::ConstructorDeclNotFound:
|
|
||||||
return "constructor declaration not found";
|
|
||||||
|
|
||||||
case EscapesFunctionError::FunctionDeclNotFound:
|
|
||||||
return "function declaration not found";
|
|
||||||
|
|
||||||
case EscapesFunctionError::FunctionIsBuiltin:
|
|
||||||
return "function is builtin";
|
|
||||||
|
|
||||||
case EscapesFunctionError::FunctionIsVariadic:
|
|
||||||
return "function is variadic";
|
|
||||||
|
|
||||||
case EscapesFunctionError::ExprNotInCall:
|
|
||||||
return "expression is not in call";
|
|
||||||
|
|
||||||
case EscapesFunctionError::NoParamForArg:
|
|
||||||
return "no parameter for argument";
|
|
||||||
|
|
||||||
case EscapesFunctionError::ArgAndParamNotPointers:
|
|
||||||
return "argument and parameter are not pointers";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const EscapesFunctionErrorCategory TheEscapesFunctionErrorCategory {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::error_code make_error_code(EscapesFunctionError e)
|
std::string EscapesFunctionErrorCategory::message(int ev) const {
|
||||||
{
|
switch (static_cast<EscapesFunctionError>(ev)) {
|
||||||
|
case EscapesFunctionError::ConstructorDeclNotFound:
|
||||||
|
return "constructor declaration not found";
|
||||||
|
|
||||||
|
case EscapesFunctionError::FunctionDeclNotFound:
|
||||||
|
return "function declaration not found";
|
||||||
|
|
||||||
|
case EscapesFunctionError::FunctionIsBuiltin:
|
||||||
|
return "function is builtin";
|
||||||
|
|
||||||
|
case EscapesFunctionError::FunctionIsVariadic:
|
||||||
|
return "function is variadic";
|
||||||
|
|
||||||
|
case EscapesFunctionError::ExprNotInCall:
|
||||||
|
return "expression is not in call";
|
||||||
|
|
||||||
|
case EscapesFunctionError::NoParamForArg:
|
||||||
|
return "no parameter for argument";
|
||||||
|
|
||||||
|
case EscapesFunctionError::ArgAndParamNotPointers:
|
||||||
|
return "argument and parameter are not pointers";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const EscapesFunctionErrorCategory TheEscapesFunctionErrorCategory{};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
std::error_code make_error_code(EscapesFunctionError e) {
|
||||||
return {static_cast<int>(e), TheEscapesFunctionErrorCategory};
|
return {static_cast<int>(e), TheEscapesFunctionErrorCategory};
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<std::tuple<const Stmt*, const Decl*>>
|
ErrorOr<std::tuple<const Stmt *, const Decl *>>
|
||||||
escapesFunction(const Expr* Arg, const CXXConstructExpr* Construct) {
|
escapesFunction(const Expr *Arg, const CXXConstructExpr *Construct) {
|
||||||
// We get the function declaration corresponding to the call.
|
// We get the function declaration corresponding to the call.
|
||||||
auto CtorDecl = Construct->getConstructor();
|
auto CtorDecl = Construct->getConstructor();
|
||||||
if (!CtorDecl) {
|
if (!CtorDecl) {
|
||||||
@@ -138,8 +129,8 @@ escapesFunction(const Expr* Arg, const CXXConstructExpr* Construct) {
|
|||||||
Construct->getNumArgs());
|
Construct->getNumArgs());
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<std::tuple<const Stmt*, const Decl*>>
|
ErrorOr<std::tuple<const Stmt *, const Decl *>>
|
||||||
escapesFunction(const Expr* Arg, const CallExpr* Call) {
|
escapesFunction(const Expr *Arg, const CallExpr *Call) {
|
||||||
// We get the function declaration corresponding to the call.
|
// We get the function declaration corresponding to the call.
|
||||||
auto FuncDecl = Call->getDirectCallee();
|
auto FuncDecl = Call->getDirectCallee();
|
||||||
if (!FuncDecl) {
|
if (!FuncDecl) {
|
||||||
@@ -149,8 +140,8 @@ escapesFunction(const Expr* Arg, const CallExpr* Call) {
|
|||||||
return escapesFunction(Arg, FuncDecl, Call->getArgs(), Call->getNumArgs());
|
return escapesFunction(Arg, FuncDecl, Call->getArgs(), Call->getNumArgs());
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<std::tuple<const Stmt*, const Decl*>>
|
ErrorOr<std::tuple<const Stmt *, const Decl *>>
|
||||||
escapesFunction(const Expr* Arg, const CXXOperatorCallExpr* OpCall) {
|
escapesFunction(const Expr *Arg, const CXXOperatorCallExpr *OpCall) {
|
||||||
// We get the function declaration corresponding to the operator call.
|
// We get the function declaration corresponding to the operator call.
|
||||||
auto FuncDecl = OpCall->getDirectCallee();
|
auto FuncDecl = OpCall->getDirectCallee();
|
||||||
if (!FuncDecl) {
|
if (!FuncDecl) {
|
||||||
@@ -170,11 +161,11 @@ escapesFunction(const Expr* Arg, const CXXOperatorCallExpr* OpCall) {
|
|||||||
return escapesFunction(Arg, FuncDecl, Args, NumArgs);
|
return escapesFunction(Arg, FuncDecl, Args, NumArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<std::tuple<const Stmt*, const Decl*>>
|
ErrorOr<std::tuple<const Stmt *, const Decl *>>
|
||||||
escapesFunction(const Expr* Arg, const FunctionDecl *FuncDecl,
|
escapesFunction(const Expr *Arg, const FunctionDecl *FuncDecl,
|
||||||
const Expr* const* Arguments, unsigned NumArgs) {
|
const Expr *const *Arguments, unsigned NumArgs) {
|
||||||
if (!NumArgs) {
|
if (!NumArgs) {
|
||||||
return std::make_tuple((const Stmt*)nullptr, (const Decl*)nullptr);
|
return std::make_tuple((const Stmt *)nullptr, (const Decl *)nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (FuncDecl->getBuiltinID() != 0 ||
|
if (FuncDecl->getBuiltinID() != 0 ||
|
||||||
@@ -247,7 +238,7 @@ escapesFunction(const Expr* Arg, const FunctionDecl *FuncDecl,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::make_tuple(Usage, (const Decl*)ParamDeclaration);
|
return std::make_tuple(Usage, (const Decl *)ParamDeclaration);
|
||||||
} else if (auto VarDeclaration = dyn_cast<VarDecl>(DeclRef->getDecl())) {
|
} else if (auto VarDeclaration = dyn_cast<VarDecl>(DeclRef->getDecl())) {
|
||||||
// This is the case where the parameter escapes through a global/static
|
// This is the case where the parameter escapes through a global/static
|
||||||
// variable.
|
// variable.
|
||||||
@@ -255,25 +246,26 @@ escapesFunction(const Expr* Arg, const FunctionDecl *FuncDecl,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::make_tuple(Usage, (const Decl*)VarDeclaration);
|
return std::make_tuple(Usage, (const Decl *)VarDeclaration);
|
||||||
} else if (auto FieldDeclaration = dyn_cast<FieldDecl>(DeclRef->getDecl())) {
|
} else if (auto FieldDeclaration =
|
||||||
|
dyn_cast<FieldDecl>(DeclRef->getDecl())) {
|
||||||
// This is the case where the parameter escapes through a field.
|
// This is the case where the parameter escapes through a field.
|
||||||
|
|
||||||
return std::make_tuple(Usage, (const Decl*)FieldDeclaration);
|
return std::make_tuple(Usage, (const Decl *)FieldDeclaration);
|
||||||
}
|
}
|
||||||
} else if (isa<ReturnStmt>(Usage)) {
|
} else if (isa<ReturnStmt>(Usage)) {
|
||||||
// This is the case where the parameter escapes through the return value
|
// This is the case where the parameter escapes through the return value
|
||||||
// of the function.
|
// of the function.
|
||||||
if (!FuncDecl->getReturnType()->isPointerType()
|
if (!FuncDecl->getReturnType()->isPointerType() &&
|
||||||
&& !FuncDecl->getReturnType()->isReferenceType()) {
|
!FuncDecl->getReturnType()->isReferenceType()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::make_tuple(Usage, (const Decl*)FuncDecl);
|
return std::make_tuple(Usage, (const Decl *)FuncDecl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// No early-return, this means that we haven't found any case of funciton
|
// No early-return, this means that we haven't found any case of funciton
|
||||||
// escaping and that therefore the parameter remains in the function scope.
|
// escaping and that therefore the parameter remains in the function scope.
|
||||||
return std::make_tuple((const Stmt*)nullptr, (const Decl*)nullptr);
|
return std::make_tuple((const Stmt *)nullptr, (const Decl *)nullptr);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
|
||||||
#ifndef VariableUsageHelpers_h__
|
#ifndef VariableUsageHelpers_h__
|
||||||
#define VariableUsageHelpers_h__
|
#define VariableUsageHelpers_h__
|
||||||
|
|
||||||
@@ -14,9 +13,8 @@
|
|||||||
/// WARNING: incomplete behaviour/implementation for general-purpose use outside
|
/// WARNING: incomplete behaviour/implementation for general-purpose use outside
|
||||||
/// of escapesFunction(). This only detects very basic usages (see
|
/// of escapesFunction(). This only detects very basic usages (see
|
||||||
/// implementation for more details).
|
/// implementation for more details).
|
||||||
std::vector<const Stmt*>
|
std::vector<const Stmt *> getUsageAsRvalue(const ValueDecl *ValueDeclaration,
|
||||||
getUsageAsRvalue(const ValueDecl* ValueDeclaration,
|
const FunctionDecl *FuncDecl);
|
||||||
const FunctionDecl* FuncDecl);
|
|
||||||
|
|
||||||
/// This is the error enumeration for escapesFunction(), describing all the
|
/// This is the error enumeration for escapesFunction(), describing all the
|
||||||
/// possible error cases.
|
/// possible error cases.
|
||||||
@@ -46,20 +44,20 @@ std::error_code make_error_code(EscapesFunctionError);
|
|||||||
/// WARNING: incomplete behaviour/implementation for general-purpose use outside
|
/// WARNING: incomplete behaviour/implementation for general-purpose use outside
|
||||||
/// of DanglingOnTemporaryChecker. This only covers a limited set of cases,
|
/// of DanglingOnTemporaryChecker. This only covers a limited set of cases,
|
||||||
/// mainly in terms of arguments and parameter types.
|
/// mainly in terms of arguments and parameter types.
|
||||||
ErrorOr<std::tuple<const Stmt*, const Decl*>>
|
ErrorOr<std::tuple<const Stmt *, const Decl *>>
|
||||||
escapesFunction(const Expr* Arg, const FunctionDecl *FuncDecl,
|
escapesFunction(const Expr *Arg, const FunctionDecl *FuncDecl,
|
||||||
const Expr* const* Arguments, unsigned NumArgs);
|
const Expr *const *Arguments, unsigned NumArgs);
|
||||||
|
|
||||||
/// Helper function taking a call expression.
|
/// Helper function taking a call expression.
|
||||||
ErrorOr<std::tuple<const Stmt*, const Decl*>>
|
ErrorOr<std::tuple<const Stmt *, const Decl *>>
|
||||||
escapesFunction(const Expr* Arg, const CallExpr* Call);
|
escapesFunction(const Expr *Arg, const CallExpr *Call);
|
||||||
|
|
||||||
/// Helper function taking a construct expression.
|
/// Helper function taking a construct expression.
|
||||||
ErrorOr<std::tuple<const Stmt*, const Decl*>>
|
ErrorOr<std::tuple<const Stmt *, const Decl *>>
|
||||||
escapesFunction(const Expr* Arg, const CXXConstructExpr* Construct);
|
escapesFunction(const Expr *Arg, const CXXConstructExpr *Construct);
|
||||||
|
|
||||||
/// Helper function taking an operator call expression.
|
/// Helper function taking an operator call expression.
|
||||||
ErrorOr<std::tuple<const Stmt*, const Decl*>>
|
ErrorOr<std::tuple<const Stmt *, const Decl *>>
|
||||||
escapesFunction(const Expr* Arg, const CXXOperatorCallExpr* OpCall);
|
escapesFunction(const Expr *Arg, const CXXOperatorCallExpr *OpCall);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -5,12 +5,12 @@
|
|||||||
#ifndef plugin_h__
|
#ifndef plugin_h__
|
||||||
#define plugin_h__
|
#define plugin_h__
|
||||||
|
|
||||||
#include "clang/Analysis/CFG.h"
|
|
||||||
#include "clang/AST/ASTConsumer.h"
|
#include "clang/AST/ASTConsumer.h"
|
||||||
#include "clang/AST/ASTContext.h"
|
#include "clang/AST/ASTContext.h"
|
||||||
#include "clang/AST/RecursiveASTVisitor.h"
|
#include "clang/AST/RecursiveASTVisitor.h"
|
||||||
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
||||||
#include "clang/ASTMatchers/ASTMatchers.h"
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
||||||
|
#include "clang/Analysis/CFG.h"
|
||||||
#include "clang/Basic/Version.h"
|
#include "clang/Basic/Version.h"
|
||||||
#include "clang/Frontend/CompilerInstance.h"
|
#include "clang/Frontend/CompilerInstance.h"
|
||||||
#include "clang/Frontend/MultiplexConsumer.h"
|
#include "clang/Frontend/MultiplexConsumer.h"
|
||||||
@@ -18,8 +18,8 @@
|
|||||||
#include "llvm/ADT/DenseMap.h"
|
#include "llvm/ADT/DenseMap.h"
|
||||||
#include "llvm/Support/FileSystem.h"
|
#include "llvm/Support/FileSystem.h"
|
||||||
#include "llvm/Support/Path.h"
|
#include "llvm/Support/Path.h"
|
||||||
#include <memory>
|
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#define CLANG_VERSION_FULL (CLANG_VERSION_MAJOR * 100 + CLANG_VERSION_MINOR)
|
#define CLANG_VERSION_FULL (CLANG_VERSION_MAJOR * 100 + CLANG_VERSION_MINOR)
|
||||||
|
|
||||||
@@ -54,9 +54,9 @@ typedef ASTConsumer *ASTConsumerPtr;
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// In order to support running our checks using clang-tidy, we implement a source
|
// In order to support running our checks using clang-tidy, we implement a
|
||||||
// compatible base check class called BaseCheck, and we use the preprocessor to
|
// source compatible base check class called BaseCheck, and we use the
|
||||||
// decide which base class to pick.
|
// preprocessor to decide which base class to pick.
|
||||||
#ifdef CLANG_TIDY
|
#ifdef CLANG_TIDY
|
||||||
#include "../ClangTidy.h"
|
#include "../ClangTidy.h"
|
||||||
typedef clang::tidy::ClangTidyCheck BaseCheck;
|
typedef clang::tidy::ClangTidyCheck BaseCheck;
|
||||||
|
|||||||
Reference in New Issue
Block a user