From 7182da5c8e40d4d89ba852f4a82a527b355a4a96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 11 Nov 2020 09:15:36 +0100 Subject: [PATCH] astyle formatting --- lib/analyzer.h | 56 ++++++++++++++------- lib/astutils.cpp | 10 +++- lib/forwardanalyzer.cpp | 33 ++++++------ lib/forwardanalyzer.h | 6 +-- lib/reverseanalyzer.cpp | 37 ++++++-------- lib/valueflow.cpp | 106 ++++++++++++++++++--------------------- lib/valueflow.h | 9 ++-- test/testnullpointer.cpp | 12 ++--- 8 files changed, 139 insertions(+), 130 deletions(-) diff --git a/lib/analyzer.h b/lib/analyzer.h index 60f8bd359..3e676b662 100644 --- a/lib/analyzer.h +++ b/lib/analyzer.h @@ -44,43 +44,65 @@ struct Analyzer { Idempotent = (1 << 5), }; - void set(unsigned int f, bool state = true) { mFlag = state ? mFlag | f : mFlag & ~f; } + void set(unsigned int f, bool state = true) { + mFlag = state ? mFlag | f : mFlag & ~f; + } - bool get(unsigned int f) const { return ((mFlag & f) != 0); } + bool get(unsigned int f) const { + return ((mFlag & f) != 0); + } - bool isRead() const { return get(Read); } + bool isRead() const { + return get(Read); + } - bool isWrite() const { return get(Write); } + bool isWrite() const { + return get(Write); + } - bool isInvalid() const { return get(Invalid); } + bool isInvalid() const { + return get(Invalid); + } - bool isInconclusive() const { return get(Inconclusive); } + bool isInconclusive() const { + return get(Inconclusive); + } - bool isNone() const { return mFlag == None; } + bool isNone() const { + return mFlag == None; + } - bool isModified() const { return isWrite() || isInvalid(); } + bool isModified() const { + return isWrite() || isInvalid(); + } - bool isIdempotent() const { return get(Idempotent); } + bool isIdempotent() const { + return get(Idempotent); + } - bool matches() const { return get(Match); } + bool matches() const { + return get(Match); + } - Action& operator|=(Action a) - { + Action& operator|=(Action a) { set(a.mFlag); return *this; } - friend Action operator|(Action a, Action b) - { + friend Action operator|(Action a, Action b) { a |= b; return a; } - friend bool operator==(Action a, Action b) { return a.mFlag == b.mFlag; } + friend bool operator==(Action a, Action b) { + return a.mFlag == b.mFlag; + } - friend bool operator!=(Action a, Action b) { return a.mFlag != b.mFlag; } + friend bool operator!=(Action a, Action b) { + return a.mFlag != b.mFlag; + } - private: + private: unsigned int mFlag; }; diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 9f12df3d0..667717058 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -315,8 +315,14 @@ static T* previousBeforeAstLeftmostLeafGeneric(T* tok) return leftmostLeaf->previous(); } -const Token* previousBeforeAstLeftmostLeaf(const Token* tok) { return previousBeforeAstLeftmostLeafGeneric(tok); } -Token* previousBeforeAstLeftmostLeaf(Token* tok) { return previousBeforeAstLeftmostLeafGeneric(tok); } +const Token* previousBeforeAstLeftmostLeaf(const Token* tok) +{ + return previousBeforeAstLeftmostLeafGeneric(tok); +} +Token* previousBeforeAstLeftmostLeaf(Token* tok) +{ + return previousBeforeAstLeftmostLeafGeneric(tok); +} template )> static T* nextAfterAstRightmostLeafGeneric(T* tok) diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index a4e9a26f0..08806a9db 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -26,8 +26,12 @@ struct ForwardTraversal { std::pair evalCond(const Token* tok) { std::vector result = analyzer->evaluate(tok); // TODO: We should convert to bool - bool checkThen = std::any_of(result.begin(), result.end(), [](int x) { return x == 1; }); - bool checkElse = std::any_of(result.begin(), result.end(), [](int x) { return x == 0; }); + bool checkThen = std::any_of(result.begin(), result.end(), [](int x) { + return x == 1; + }); + bool checkElse = std::any_of(result.begin(), result.end(), [](int x) { + return x == 0; + }); return std::make_pair(checkThen, checkElse); } @@ -150,8 +154,7 @@ struct ForwardTraversal { } template - T* findRange(T* start, const Token* end, std::function pred) - { + T* findRange(T* start, const Token* end, std::function pred) { for (T* tok = start; tok && tok != end; tok = tok->next()) { Analyzer::Action action = analyzer->analyze(tok, Analyzer::Direction::Forward); if (pred(action)) @@ -160,8 +163,7 @@ struct ForwardTraversal { return nullptr; } - Analyzer::Action analyzeRecursive(const Token* start) - { + Analyzer::Action analyzeRecursive(const Token* start) { Analyzer::Action result = Analyzer::Action::None; std::function f = [&](const Token* tok) { result = analyzer->analyze(tok, Analyzer::Direction::Forward); @@ -173,8 +175,7 @@ struct ForwardTraversal { return result; } - Analyzer::Action analyzeRange(const Token* start, const Token* end) - { + Analyzer::Action analyzeRange(const Token* start, const Token* end) { Analyzer::Action result = Analyzer::Action::None; for (const Token* tok = start; tok && tok != end; tok = tok->next()) { Analyzer::Action action = analyzer->analyze(tok, Analyzer::Direction::Forward); @@ -211,17 +212,17 @@ struct ForwardTraversal { Inconclusive, }; - Analyzer::Action analyzeScope(const Token* endBlock) { return analyzeRange(endBlock->link(), endBlock); } + Analyzer::Action analyzeScope(const Token* endBlock) { + return analyzeRange(endBlock->link(), endBlock); + } - Analyzer::Action checkScope(Token* endBlock) - { + Analyzer::Action checkScope(Token* endBlock) { Analyzer::Action a = analyzeScope(endBlock); forkScope(endBlock, a.isModified()); return a; } - Analyzer::Action checkScope(const Token* endBlock) - { + Analyzer::Action checkScope(const Token* endBlock) { Analyzer::Action a = analyzeScope(endBlock); return a; } @@ -568,9 +569,9 @@ struct ForwardTraversal { }; Analyzer::Action valueFlowGenericForward(Token* start, - const Token* end, - const ValuePtr& a, - const Settings* settings) + const Token* end, + const ValuePtr& a, + const Settings* settings) { ForwardTraversal ft{a, settings}; ft.updateRange(start, end); diff --git a/lib/forwardanalyzer.h b/lib/forwardanalyzer.h index f4388bee8..87828696e 100644 --- a/lib/forwardanalyzer.h +++ b/lib/forwardanalyzer.h @@ -27,9 +27,9 @@ class Token; template class ValuePtr; Analyzer::Action valueFlowGenericForward(Token* start, - const Token* end, - const ValuePtr& a, - const Settings* settings); + const Token* end, + const ValuePtr& a, + const Settings* settings); Analyzer::Action valueFlowGenericForward(Token* start, const ValuePtr& a, const Settings* settings); diff --git a/lib/reverseanalyzer.cpp b/lib/reverseanalyzer.cpp index dfe4fe290..314490ef8 100644 --- a/lib/reverseanalyzer.cpp +++ b/lib/reverseanalyzer.cpp @@ -17,17 +17,19 @@ struct ReverseTraversal { ValuePtr analyzer; const Settings* settings; - std::pair evalCond(const Token* tok) - { + std::pair evalCond(const Token* tok) { std::vector result = analyzer->evaluate(tok); // TODO: We should convert to bool - bool checkThen = std::any_of(result.begin(), result.end(), [](int x) { return x == 1; }); - bool checkElse = std::any_of(result.begin(), result.end(), [](int x) { return x == 0; }); + bool checkThen = std::any_of(result.begin(), result.end(), [](int x) { + return x == 1; + }); + bool checkElse = std::any_of(result.begin(), result.end(), [](int x) { + return x == 0; + }); return std::make_pair(checkThen, checkElse); } - bool update(Token* tok) - { + bool update(Token* tok) { Analyzer::Action action = analyzer->analyze(tok, Analyzer::Direction::Reverse); if (!action.isNone()) analyzer->update(tok, action, Analyzer::Direction::Reverse); @@ -38,8 +40,7 @@ struct ReverseTraversal { return true; } - bool updateRecursive(Token* start) - { + bool updateRecursive(Token* start) { bool continueB = true; visitAstNodes(start, [&](Token* tok) { continueB &= update(tok); @@ -51,8 +52,7 @@ struct ReverseTraversal { return continueB; } - Analyzer::Action analyzeRecursive(const Token* start) - { + Analyzer::Action analyzeRecursive(const Token* start) { Analyzer::Action result = Analyzer::Action::None; visitAstNodes(start, [&](const Token* tok) { result |= analyzer->analyze(tok, Analyzer::Direction::Reverse); @@ -63,8 +63,7 @@ struct ReverseTraversal { return result; } - Analyzer::Action analyzeRange(const Token* start, const Token* end) - { + Analyzer::Action analyzeRange(const Token* start, const Token* end) { Analyzer::Action result = Analyzer::Action::None; for (const Token* tok = start; tok && tok != end; tok = tok->next()) { Analyzer::Action action = analyzer->analyze(tok, Analyzer::Direction::Reverse); @@ -75,8 +74,7 @@ struct ReverseTraversal { return result; } - Token* isDeadCode(Token* tok) - { + Token* isDeadCode(Token* tok) { int opSide = 0; for (; tok && tok->astParent(); tok = tok->astParent()) { Token* parent = tok->astParent(); @@ -118,11 +116,10 @@ struct ReverseTraversal { return nullptr; } - void traverse(Token* start) - { + void traverse(Token* start) { for (Token* tok = start->previous(); tok; tok = tok->previous()) { if (tok == start || (tok->str() == "{" && (tok->scope()->type == Scope::ScopeType::eFunction || - tok->scope()->type == Scope::ScopeType::eLambda))) { + tok->scope()->type == Scope::ScopeType::eLambda))) { break; } if (Token::Match(tok, "return|break|continue")) @@ -256,8 +253,7 @@ struct ReverseTraversal { } } - static Token* assignExpr(Token* tok) - { + static Token* assignExpr(Token* tok) { while (tok->astParent() && (astIsRHS(tok) || !tok->astParent()->isBinaryOp())) { if (tok->astParent()->isAssignmentOp()) return tok->astParent(); @@ -266,8 +262,7 @@ struct ReverseTraversal { return nullptr; } - static Token* isUnevaluated(Token* tok) - { + static Token* isUnevaluated(Token* tok) { if (Token::Match(tok, ")|>") && tok->link()) { Token* start = tok->link(); if (Token::Match(start->previous(), "sizeof|decltype (")) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index b57316726..48deb053a 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1741,11 +1741,11 @@ static void valueFlowGlobalStaticVar(TokenList *tokenList, const Settings *setti } static Analyzer::Action valueFlowForwardVariable(Token* const startToken, - const Token* const endToken, - const Variable* const var, - std::list values, - TokenList* const tokenlist, - const Settings* const settings); + const Token* const endToken, + const Variable* const var, + std::list values, + TokenList* const tokenlist, + const Settings* const settings); // Old deprecated version static void valueFlowForward(Token* startToken, @@ -1908,7 +1908,8 @@ static void valueFlowAST(Token *tok, nonneg int varid, const ValueFlow::Value &v static const std::string& invertAssign(const std::string& assign) { static std::unordered_map lookup = { - {"+=", "-="}, {"-=", "+="}, {"*=", "/="}, {"/=", "*="}, {"<<=", ">>="}, {">>=", "<<="}, {"^=", "^="}}; + {"+=", "-="}, {"-=", "+="}, {"*=", "/="}, {"/=", "*="}, {"<<=", ">>="}, {">>=", "<<="}, {"^=", "^="} + }; static std::string empty = ""; auto it = lookup.find(assign); if (it == lookup.end()) @@ -2142,8 +2143,7 @@ struct ValueFlowAnalyzer : Analyzer { return Action::None; } - virtual Action isWritable(const Token* tok, Direction d) const - { + virtual Action isWritable(const Token* tok, Direction d) const { const ValueFlow::Value* value = getValue(tok); if (!value) return Action::None; @@ -2179,16 +2179,14 @@ struct ValueFlowAnalyzer : Analyzer { return Action::None; } - static const std::string& getAssign(const Token* tok, Direction d) - { + static const std::string& getAssign(const Token* tok, Direction d) { if (d == Direction::Forward) return tok->str(); else return invertAssign(tok->str()); } - virtual void writeValue(ValueFlow::Value* value, const Token* tok, Direction d) const - { + virtual void writeValue(ValueFlow::Value* value, const Token* tok, Direction d) const { if (!value) return; if (!tok->astParent()) @@ -2218,8 +2216,7 @@ struct ValueFlowAnalyzer : Analyzer { } } - virtual Action analyze(const Token* tok, Direction d) const OVERRIDE - { + virtual Action analyze(const Token* tok, Direction d) const OVERRIDE { if (invalid()) return Action::Invalid; bool inconclusive = false; @@ -2306,8 +2303,7 @@ struct ValueFlowAnalyzer : Analyzer { makeConditional(); } - virtual void update(Token* tok, Action a, Direction d) OVERRIDE - { + virtual void update(Token* tok, Action a, Direction d) OVERRIDE { ValueFlow::Value* value = getValue(tok); if (!value) return; @@ -2324,7 +2320,9 @@ struct ValueFlowAnalyzer : Analyzer { setTokenValue(tok, *value, getSettings()); } - virtual ValuePtr reanalyze(Token*, const std::string&) const OVERRIDE { return {}; } + virtual ValuePtr reanalyze(Token*, const std::string&) const OVERRIDE { + return {}; + } }; ValuePtr makeAnalyzer(Token* exprTok, const ValueFlow::Value& value, const TokenList* tokenlist); @@ -2432,8 +2430,7 @@ struct SingleValueFlowAnalyzer : ValueFlowAnalyzer { return false; } - virtual ValuePtr reanalyze(Token* tok, const std::string& msg) const OVERRIDE - { + virtual ValuePtr reanalyze(Token* tok, const std::string& msg) const OVERRIDE { ValueFlow::Value newValue = value; newValue.errorPath.emplace_back(tok, msg); return makeAnalyzer(tok, newValue, tokenlist); @@ -2449,8 +2446,7 @@ struct VariableAnalyzer : SingleValueFlowAnalyzer { const ValueFlow::Value& val, std::vector paliases, const TokenList* t) - : SingleValueFlowAnalyzer(val, t), var(v) - { + : SingleValueFlowAnalyzer(val, t), var(v) { varids[var->declarationId()] = var; for (const Variable* av:paliases) { if (!av) @@ -2500,12 +2496,12 @@ static std::vector getAliasesFromValues(std::list values, - std::vector aliases, - TokenList* const tokenlist, - const Settings* const settings) + const Token* const endToken, + const Variable* const var, + std::list values, + std::vector aliases, + TokenList* const tokenlist, + const Settings* const settings) { Analyzer::Action actions; for (ValueFlow::Value& v : values) { @@ -2516,11 +2512,11 @@ static Analyzer::Action valueFlowForwardVariable(Token* const startToken, } static Analyzer::Action valueFlowForwardVariable(Token* const startToken, - const Token* const endToken, - const Variable* const var, - std::list values, - TokenList* const tokenlist, - const Settings* const settings) + const Token* const endToken, + const Variable* const var, + std::list values, + TokenList* const tokenlist, + const Settings* const settings) { return valueFlowForwardVariable( startToken, endToken, var, std::move(values), getAliasesFromValues(values), tokenlist, settings); @@ -2550,8 +2546,7 @@ struct ExpressionAnalyzer : SingleValueFlowAnalyzer { ExpressionAnalyzer() : SingleValueFlowAnalyzer(), expr(nullptr), local(true), unknown(false) {} ExpressionAnalyzer(const Token* e, const ValueFlow::Value& val, const TokenList* t) - : SingleValueFlowAnalyzer(val, t), expr(e), local(true), unknown(false) - { + : SingleValueFlowAnalyzer(val, t), expr(e), local(true), unknown(false) { setupExprVarIds(); } @@ -2610,11 +2605,11 @@ struct ExpressionAnalyzer : SingleValueFlowAnalyzer { }; static Analyzer::Action valueFlowForwardExpression(Token* startToken, - const Token* endToken, - const Token* exprTok, - const std::list& values, - const TokenList* const tokenlist, - const Settings* settings) + const Token* endToken, + const Token* exprTok, + const std::list& values, + const TokenList* const tokenlist, + const Settings* settings) { Analyzer::Action actions; for (const ValueFlow::Value& v : values) { @@ -2697,11 +2692,11 @@ ValuePtr makeAnalyzer(Token* exprTok, const ValueFlow::Value& value, c } static Analyzer::Action valueFlowForward(Token* startToken, - const Token* endToken, - const Token* exprTok, - std::list values, - TokenList* const tokenlist, - const Settings* settings) + const Token* endToken, + const Token* exprTok, + std::list values, + TokenList* const tokenlist, + const Settings* settings) { const Token* expr = solveExprValues(exprTok, values); if (expr->variable()) { @@ -4923,8 +4918,7 @@ struct MultiValueFlowAnalyzer : ValueFlowAnalyzer { MultiValueFlowAnalyzer() : ValueFlowAnalyzer(), values(), vars() {} MultiValueFlowAnalyzer(const std::unordered_map& args, const TokenList* t) - : ValueFlowAnalyzer(t), values(), vars() - { + : ValueFlowAnalyzer(t), values(), vars() { for (const auto& p:args) { values[p.first->declarationId()] = p.second; vars[p.first->declarationId()] = p.first; @@ -5774,8 +5768,7 @@ struct ContainerVariableAnalyzer : VariableAnalyzer { return tok->varId() == var->declarationId() || (astIsIterator(tok) && isAliasOf(tok, var->declarationId())); } - virtual Action isWritable(const Token* tok, Direction d) const OVERRIDE - { + virtual Action isWritable(const Token* tok, Direction d) const OVERRIDE { if (astIsIterator(tok)) return Action::None; if (d == Direction::Reverse) @@ -5805,8 +5798,7 @@ struct ContainerVariableAnalyzer : VariableAnalyzer { return Action::None; } - virtual void writeValue(ValueFlow::Value* value, const Token* tok, Direction d) const OVERRIDE - { + virtual void writeValue(ValueFlow::Value* value, const Token* tok, Direction d) const OVERRIDE { if (d == Direction::Reverse) return; if (!value) @@ -5853,18 +5845,18 @@ struct ContainerVariableAnalyzer : VariableAnalyzer { }; static Analyzer::Action valueFlowContainerForward(Token* tok, - const Token* endToken, - const Variable* var, - ValueFlow::Value value, - TokenList* tokenlist) + const Token* endToken, + const Variable* var, + ValueFlow::Value value, + TokenList* tokenlist) { ContainerVariableAnalyzer a(var, value, getAliasesFromValues({value}), tokenlist); return valueFlowGenericForward(tok, endToken, a, tokenlist->getSettings()); } static Analyzer::Action valueFlowContainerForward(Token* tok, - const Variable* var, - ValueFlow::Value value, - TokenList* tokenlist) + const Variable* var, + ValueFlow::Value value, + TokenList* tokenlist) { const Token * endOfVarScope = nullptr; if (var->isLocal() || var->isArgument()) diff --git a/lib/valueflow.h b/lib/valueflow.h index bcee6de31..edbc5ea71 100644 --- a/lib/valueflow.h +++ b/lib/valueflow.h @@ -56,8 +56,7 @@ namespace ValueFlow { struct equalVisitor { template - void operator()(bool& result, T x, U y) const - { + void operator()(bool& result, T x, U y) const { result = !(x > y || x < y); } }; @@ -122,8 +121,7 @@ namespace ValueFlow { } template - static void visitValue(T& self, F f) - { + static void visitValue(T& self, F f) { switch (self.valueType) { case ValueType::INT: case ValueType::BUFFER_SIZE: @@ -163,8 +161,7 @@ namespace ValueFlow { } template )> - bool equalTo(const T& x) const - { + bool equalTo(const T& x) const { bool result = false; visitValue(*this, std::bind(equalVisitor{}, std::ref(result), x, std::placeholders::_1)); return result; diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 66f75ffdb..86cbd1c93 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -1899,8 +1899,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void nullpointer61() - { + void nullpointer61() { check("struct a {\n" " int *e;\n" "};\n" @@ -1933,8 +1932,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void nullpointer62() - { + void nullpointer62() { check("struct A {\n" " bool f()() const;\n" "};\n" @@ -1964,8 +1962,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void nullpointer63() - { + void nullpointer63() { check("struct A {\n" " A* a() const;\n" " A* b() const;\n" @@ -1979,8 +1976,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void nullpointer64() - { + void nullpointer64() { check("struct A {\n" " A* f() const;\n" " int g() const;\n"