Bug 1410472 - clang-plugin follows the LLVM coding style for real r=mystor

MozReview-Commit-ID: AXrQEjWzxvg
This commit is contained in:
Sylvestre Ledru
2017-10-20 19:11:50 +02:00
parent 480c1a8de7
commit 173e982fb0
67 changed files with 601 additions and 667 deletions

View File

@@ -5,7 +5,7 @@
#include "CanRunScriptChecker.h"
#include "CustomMatchers.h"
void CanRunScriptChecker::registerMatchers(MatchFinder* AstMatcher) {
void CanRunScriptChecker::registerMatchers(MatchFinder *AstMatcher) {
auto InvalidArg =
// We want to find any expression,
ignoreTrivials(expr(
@@ -19,17 +19,15 @@ void CanRunScriptChecker::registerMatchers(MatchFinder* AstMatcher) {
// and which is not a parameter of the parent function,
unless(declRefExpr(to(parmVarDecl()))),
// and which is not a MOZ_KnownLive wrapped value.
unless(callExpr(callee(
functionDecl(hasName("MOZ_KnownLive"))))),
unless(callExpr(callee(functionDecl(hasName("MOZ_KnownLive"))))),
expr().bind("invalidArg")));
auto OptionalInvalidExplicitArg =
anyOf(
// We want to find any argument which is invalid.
hasAnyArgument(InvalidArg),
auto OptionalInvalidExplicitArg = anyOf(
// We want to find any argument which is invalid.
hasAnyArgument(InvalidArg),
// This makes this matcher optional.
anything());
// This makes this matcher optional.
anything());
// Please not that the hasCanRunScriptAnnotation() matchers are not present
// directly in the cxxMemberCallExpr, callExpr and constructExpr matchers
@@ -46,9 +44,7 @@ void CanRunScriptChecker::registerMatchers(MatchFinder* AstMatcher) {
anyOf(
// which derefs into an invalid arg,
on(cxxOperatorCallExpr(
anyOf(
hasAnyArgument(InvalidArg),
anything()))),
anyOf(hasAnyArgument(InvalidArg), anything()))),
// or is an invalid arg.
on(InvalidArg),
@@ -57,13 +53,11 @@ void CanRunScriptChecker::registerMatchers(MatchFinder* AstMatcher) {
// or a regular call expression,
callExpr(
// which optionally has an invalid arg.
OptionalInvalidExplicitArg,
expr().bind("callExpr")),
OptionalInvalidExplicitArg, expr().bind("callExpr")),
// or a construct expression,
cxxConstructExpr(
// which optionally has an invalid arg.
OptionalInvalidExplicitArg,
expr().bind("constructExpr"))),
OptionalInvalidExplicitArg, expr().bind("constructExpr"))),
anyOf(
// We want to match the parent function.
@@ -80,52 +74,52 @@ void CanRunScriptChecker::onStartOfTranslationUnit() {
}
namespace {
/// This class is a callback used internally to match function declarations
/// with the MOZ_CAN_RUN_SCRIPT annotation, adding these functions and all
/// the methods they override to the can-run-script function set.
class FuncSetCallback : public MatchFinder::MatchCallback {
public:
FuncSetCallback(std::unordered_set<const FunctionDecl*> &FuncSet)
/// This class is a callback used internally to match function declarations
/// with the MOZ_CAN_RUN_SCRIPT annotation, adding these functions and all
/// the methods they override to the can-run-script function set.
class FuncSetCallback : public MatchFinder::MatchCallback {
public:
FuncSetCallback(std::unordered_set<const FunctionDecl *> &FuncSet)
: CanRunScriptFuncs(FuncSet) {}
void run(const MatchFinder::MatchResult &Result) override;
void run(const MatchFinder::MatchResult &Result) override;
private:
/// This method recursively adds all the methods overriden by the given
/// paremeter.
void addAllOverriddenMethodsRecursively(const CXXMethodDecl* Method);
private:
/// This method recursively adds all the methods overriden by the given
/// paremeter.
void addAllOverriddenMethodsRecursively(const CXXMethodDecl *Method);
std::unordered_set<const FunctionDecl*> &CanRunScriptFuncs;
};
std::unordered_set<const FunctionDecl *> &CanRunScriptFuncs;
};
void FuncSetCallback::run(const MatchFinder::MatchResult &Result) {
const FunctionDecl* Func =
void FuncSetCallback::run(const MatchFinder::MatchResult &Result) {
const FunctionDecl *Func =
Result.Nodes.getNodeAs<FunctionDecl>("canRunScriptFunction");
CanRunScriptFuncs.insert(Func);
CanRunScriptFuncs.insert(Func);
// If this is a method, we check the methods it overrides.
if (auto* Method = dyn_cast<CXXMethodDecl>(Func)) {
addAllOverriddenMethodsRecursively(Method);
}
// If this is a method, we check the methods it overrides.
if (auto *Method = dyn_cast<CXXMethodDecl>(Func)) {
addAllOverriddenMethodsRecursively(Method);
}
}
void FuncSetCallback::addAllOverriddenMethodsRecursively(
const CXXMethodDecl* Method) {
for (auto OverriddenMethod : Method->overridden_methods()) {
CanRunScriptFuncs.insert(OverriddenMethod);
void FuncSetCallback::addAllOverriddenMethodsRecursively(
const CXXMethodDecl *Method) {
for (auto OverriddenMethod : Method->overridden_methods()) {
CanRunScriptFuncs.insert(OverriddenMethod);
// If this is not the definition, we also add the definition (if it
// exists) to the set.
if (!OverriddenMethod->isThisDeclarationADefinition()) {
if (auto Def = OverriddenMethod->getDefinition()) {
CanRunScriptFuncs.insert(Def);
}
// If this is not the definition, we also add the definition (if it
// exists) to the set.
if (!OverriddenMethod->isThisDeclarationADefinition()) {
if (auto Def = OverriddenMethod->getDefinition()) {
CanRunScriptFuncs.insert(Def);
}
addAllOverriddenMethodsRecursively(OverriddenMethod);
}
addAllOverriddenMethodsRecursively(OverriddenMethod);
}
}
} // namespace
void CanRunScriptChecker::buildFuncSet(ASTContext *Context) {
@@ -135,16 +129,15 @@ void CanRunScriptChecker::buildFuncSet(ASTContext *Context) {
// a MOZ_CAN_RUN_SCRIPT annotation.
FuncSetCallback Callback(CanRunScriptFuncs);
// We add the matcher to the finder, linking it to our callback.
Finder.addMatcher(functionDecl(hasCanRunScriptAnnotation())
.bind("canRunScriptFunction"),
&Callback);
Finder.addMatcher(
functionDecl(hasCanRunScriptAnnotation()).bind("canRunScriptFunction"),
&Callback);
// We start the analysis, given the ASTContext our main checker is in.
Finder.matchAST(*Context);
}
void CanRunScriptChecker::check(
const MatchFinder::MatchResult &Result) {
void CanRunScriptChecker::check(const MatchFinder::MatchResult &Result) {
// If the set of functions which can run script is not yet built, then build
// it.
@@ -153,39 +146,38 @@ void CanRunScriptChecker::check(
IsFuncSetBuilt = true;
}
const char* ErrorInvalidArg =
const char *ErrorInvalidArg =
"arguments must all be strong refs or parent parameters when calling a "
"function marked as MOZ_CAN_RUN_SCRIPT (including the implicit object "
"argument)";
const char* ErrorNonCanRunScriptParent =
const char *ErrorNonCanRunScriptParent =
"functions marked as MOZ_CAN_RUN_SCRIPT can only be called from "
"functions also marked as MOZ_CAN_RUN_SCRIPT";
const char* NoteNonCanRunScriptParent =
"parent function declared here";
const char *NoteNonCanRunScriptParent = "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
// as can-run-script, consider that we didn't find a match.
if (Call && (!Call->getDirectCallee() ||
!CanRunScriptFuncs.count(Call->getDirectCallee()))) {
!CanRunScriptFuncs.count(Call->getDirectCallee()))) {
Call = nullptr;
}
const CXXConstructExpr* Construct =
const CXXConstructExpr *Construct =
Result.Nodes.getNodeAs<CXXConstructExpr>("constructExpr");
// 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
// match.
if (Construct && (!Construct->getConstructor() ||
!CanRunScriptFuncs.count(Construct->getConstructor()))) {
!CanRunScriptFuncs.count(Construct->getConstructor()))) {
Construct = nullptr;
}
const FunctionDecl* ParentFunction =
const FunctionDecl *ParentFunction =
Result.Nodes.getNodeAs<FunctionDecl>("nonCanRunScriptParentFunction");
// 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.
@@ -193,7 +185,6 @@ void CanRunScriptChecker::check(
ParentFunction = nullptr;
}
// Get the call range from either the CallExpr or the ConstructExpr.
SourceRange CallRange;
if (Call) {