Skip to content

Commit a92b10c

Browse files
authored
fixes #11104 (avoid C++-only parsing when processing C code in parsedecl()) / also avoid remaining Library::detect*() calls (#5346)
1 parent 41bdd87 commit a92b10c

9 files changed

Lines changed: 36 additions & 24 deletions

File tree

lib/astutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ std::vector<ValueType> getParentValueTypes(const Token* tok, const Settings* set
738738
const ValueType* vtCont = contTok->valueType();
739739
if (!vtCont->containerTypeToken)
740740
return {};
741-
ValueType vtParent = ValueType::parseDecl(vtCont->containerTypeToken, *settings, true); // TODO: set isCpp
741+
ValueType vtParent = ValueType::parseDecl(vtCont->containerTypeToken, *settings);
742742
return {std::move(vtParent)};
743743
}
744744
if (Token::Match(tok->astParent(), "return|(|{|%assign%") && parent) {

lib/checkclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3228,7 +3228,7 @@ void CheckClass::checkThisUseAfterFree()
32283228
for (const Variable &var : classScope->varlist) {
32293229
// Find possible "self pointer".. pointer/smartpointer member variable of "self" type.
32303230
if (var.valueType() && var.valueType()->smartPointerType != classScope->definedType && var.valueType()->typeScope != classScope) {
3231-
const ValueType valueType = ValueType::parseDecl(var.typeStartToken(), *mSettings, true); // this is only called for C++
3231+
const ValueType valueType = ValueType::parseDecl(var.typeStartToken(), *mSettings);
32323232
if (valueType.smartPointerType != classScope->definedType)
32333233
continue;
32343234
}

lib/checktype.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ void CheckType::checkFloatToIntegerOverflow()
437437
while (scope && scope->type != Scope::ScopeType::eLambda && scope->type != Scope::ScopeType::eFunction)
438438
scope = scope->nestedIn;
439439
if (scope && scope->type == Scope::ScopeType::eFunction && scope->function && scope->function->retDef) {
440-
const ValueType &valueType = ValueType::parseDecl(scope->function->retDef, *mSettings, mTokenizer->isCPP());
440+
const ValueType &valueType = ValueType::parseDecl(scope->function->retDef, *mSettings);
441441
vtfloat = tok->astOperand1()->valueType();
442442
checkFloatToIntegerOverflow(tok, &valueType, vtfloat, tok->astOperand1()->values());
443443
}

lib/clangimport.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ void clangimport::AstNode::setValueType(Token *tok)
630630
if (!decl.front())
631631
break;
632632

633-
const ValueType valueType = ValueType::parseDecl(decl.front(), *mData->mSettings, true); // TODO: set isCpp
633+
const ValueType valueType = ValueType::parseDecl(decl.front(), *mData->mSettings);
634634
if (valueType.type != ValueType::Type::UNKNOWN_TYPE) {
635635
tok->setValueType(new ValueType(valueType));
636636
break;
@@ -1545,7 +1545,7 @@ static void setValues(const Tokenizer *tokenizer, const SymbolDatabase *symbolDa
15451545

15461546
for (Token *tok = const_cast<Token*>(tokenizer->tokens()); tok; tok = tok->next()) {
15471547
if (Token::simpleMatch(tok, "sizeof (")) {
1548-
ValueType vt = ValueType::parseDecl(tok->tokAt(2), *settings, tokenizer->isCPP());
1548+
ValueType vt = ValueType::parseDecl(tok->tokAt(2), *settings);
15491549
const int sz = vt.typeSize(settings->platform, true);
15501550
if (sz <= 0)
15511551
continue;

lib/symboldatabase.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,12 +2214,13 @@ void Variable::evaluate(const Settings* settings)
22142214

22152215
const Library * const lib = &settings->library;
22162216

2217+
// TODO: ValueType::parseDecl() is also performing a container lookup
22172218
bool isContainer = false;
22182219
if (mNameToken)
22192220
setFlag(fIsArray, arrayDimensions(settings, isContainer));
22202221

22212222
if (mTypeStartToken)
2222-
setValueType(ValueType::parseDecl(mTypeStartToken,*settings, true)); // TODO: set isCpp
2223+
setValueType(ValueType::parseDecl(mTypeStartToken,*settings));
22232224

22242225
const Token* tok = mTypeStartToken;
22252226
while (tok && tok->previous() && tok->previous()->isName())
@@ -2275,7 +2276,7 @@ void Variable::evaluate(const Settings* settings)
22752276
setFlag(fIsClass, !lib->podtype(strtype) && !mTypeStartToken->isStandardType() && !isEnumType() && !isPointer() && !isReference() && strtype != "...");
22762277
setFlag(fIsStlType, Token::simpleMatch(mTypeStartToken, "std ::"));
22772278
setFlag(fIsStlString, ::isStlStringType(mTypeStartToken));
2278-
setFlag(fIsSmartPointer, lib->isSmartPointer(mTypeStartToken));
2279+
setFlag(fIsSmartPointer, mTypeStartToken->isCpp() && lib->isSmartPointer(mTypeStartToken));
22792280
}
22802281
if (mAccess == AccessControl::Argument) {
22812282
tok = mNameToken;
@@ -3558,7 +3559,7 @@ bool Type::isDerivedFrom(const std::string & ancestor) const
35583559
bool Variable::arrayDimensions(const Settings* settings, bool& isContainer)
35593560
{
35603561
isContainer = false;
3561-
const Library::Container* container = settings->library.detectContainer(mTypeStartToken);
3562+
const Library::Container* container = (mTypeStartToken && mTypeStartToken->isCpp()) ? settings->library.detectContainer(mTypeStartToken) : nullptr;
35623563
if (container && container->arrayLike_indexOp && container->size_templateArgNo > 0) {
35633564
const Token* tok = Token::findsimplematch(mTypeStartToken, "<");
35643565
if (tok) {
@@ -5653,7 +5654,7 @@ const Function* SymbolDatabase::findFunction(const Token* const tok) const
56535654
return tok1->valueType()->typeScope->findFunction(tok, tok1->valueType()->constness == 1);
56545655
if (tok1 && Token::Match(tok1->previous(), "%name% (") && tok1->previous()->function() &&
56555656
tok1->previous()->function()->retDef) {
5656-
ValueType vt = ValueType::parseDecl(tok1->previous()->function()->retDef, mSettings, mIsCpp);
5657+
ValueType vt = ValueType::parseDecl(tok1->previous()->function()->retDef, mSettings);
56575658
if (vt.typeScope)
56585659
return vt.typeScope->findFunction(tok, vt.constness == 1);
56595660
} else if (Token::Match(tok1, "%var% .")) {
@@ -5667,7 +5668,7 @@ const Function* SymbolDatabase::findFunction(const Token* const tok) const
56675668
} else if (Token::simpleMatch(tok->previous()->astOperand1(), "(")) {
56685669
const Token *castTok = tok->previous()->astOperand1();
56695670
if (castTok->isCast()) {
5670-
ValueType vt = ValueType::parseDecl(castTok->next(),mSettings, mIsCpp);
5671+
ValueType vt = ValueType::parseDecl(castTok->next(),mSettings);
56715672
if (vt.typeScope)
56725673
return vt.typeScope->findFunction(tok, vt.constness == 1);
56735674
}
@@ -5697,7 +5698,7 @@ const Function* SymbolDatabase::findFunction(const Token* const tok) const
56975698
}
56985699
// Check for constructor
56995700
if (Token::Match(tok, "%name% (|{")) {
5700-
ValueType vt = ValueType::parseDecl(tok, mSettings, mIsCpp);
5701+
ValueType vt = ValueType::parseDecl(tok, mSettings);
57015702
if (vt.typeScope)
57025703
return vt.typeScope->findFunction(tok, false);
57035704
}
@@ -6991,7 +6992,7 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
69916992
}
69926993

69936994
// Construct smart pointer
6994-
else if (mSettings.library.isSmartPointer(start)) {
6995+
else if (mIsCpp && mSettings.library.isSmartPointer(start)) {
69956996
ValueType valuetype;
69966997
if (parsedecl(start, &valuetype, mDefaultSignedness, mSettings, mIsCpp)) {
69976998
setValueType(tok, valuetype);
@@ -7066,7 +7067,7 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
70667067
}
70677068
}
70687069
}
7069-
if (tok->astParent() && Token::Match(tok->astOperand1(), "%name%|::")) {
7070+
if (mIsCpp && tok->astParent() && Token::Match(tok->astOperand1(), "%name%|::")) {
70707071
const Token *typeStartToken = tok->astOperand1();
70717072
while (typeStartToken && typeStartToken->str() == "::")
70727073
typeStartToken = typeStartToken->astOperand1();
@@ -7216,7 +7217,7 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
72167217
functionScope = functionScope->nestedIn;
72177218
if (functionScope && functionScope->type == Scope::eFunction && functionScope->function &&
72187219
functionScope->function->retDef) {
7219-
ValueType vt = ValueType::parseDecl(functionScope->function->retDef, mSettings, mIsCpp);
7220+
ValueType vt = ValueType::parseDecl(functionScope->function->retDef, mSettings);
72207221
setValueType(tok, vt);
72217222
if (Token::simpleMatch(tok, "return {"))
72227223
setValueType(tok->next(), vt);
@@ -7314,10 +7315,10 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
73147315
createSymbolDatabaseSetVariablePointers();
73157316
}
73167317

7317-
ValueType ValueType::parseDecl(const Token *type, const Settings &settings, bool isCpp)
7318+
ValueType ValueType::parseDecl(const Token *type, const Settings &settings)
73187319
{
73197320
ValueType vt;
7320-
parsedecl(type, &vt, settings.platform.defaultSign == 'u' ? Sign::UNSIGNED : Sign::SIGNED, settings, isCpp);
7321+
parsedecl(type, &vt, settings.platform.defaultSign == 'u' ? Sign::UNSIGNED : Sign::SIGNED, settings, type->isCpp());
73217322
return vt;
73227323
}
73237324

lib/symboldatabase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ class CPPCHECKLIB ValueType {
12681268
originalTypeName(std::move(otn))
12691269
{}
12701270

1271-
static ValueType parseDecl(const Token *type, const Settings &settings, bool isCpp);
1271+
static ValueType parseDecl(const Token *type, const Settings &settings);
12721272

12731273
static Type typeFromString(const std::string &typestr, bool longType);
12741274

lib/token.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,3 +2586,11 @@ Token* findLambdaEndScope(Token* tok)
25862586
const Token* findLambdaEndScope(const Token* tok) {
25872587
return findLambdaEndScope(const_cast<Token*>(tok));
25882588
}
2589+
2590+
bool Token::isCpp() const
2591+
{
2592+
if (mTokensFrontBack && mTokensFrontBack->list) {
2593+
return mTokensFrontBack->list->isCPP();
2594+
}
2595+
return true; // assume C++ by default
2596+
}

lib/token.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,9 @@ class CPPCHECKLIB Token {
14841484
void setTokenDebug(TokenDebug td) {
14851485
mImpl->mDebug = td;
14861486
}
1487+
1488+
/** defaults to C++ if it cannot be determined */
1489+
bool isCpp() const;
14871490
};
14881491

14891492
Token* findTypeEnd(Token* tok);

lib/valueflow.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ static void setTokenValue(Token* tok,
760760
if (contains({ValueFlow::Value::ValueType::INT, ValueFlow::Value::ValueType::SYMBOLIC}, value.valueType) &&
761761
Token::simpleMatch(parent->astOperand1(), "dynamic_cast"))
762762
return;
763-
const ValueType &valueType = ValueType::parseDecl(castType, *settings, true); // TODO: set isCpp
763+
const ValueType &valueType = ValueType::parseDecl(castType, *settings);
764764
if (value.isImpossible() && value.isIntValue() && value.intvalue < 0 && astIsUnsigned(tok) &&
765765
valueType.sign == ValueType::SIGNED && tok->valueType() &&
766766
ValueFlow::getSizeOf(*tok->valueType(), settings) >= ValueFlow::getSizeOf(valueType, settings))
@@ -1095,7 +1095,7 @@ static void setTokenValueCast(Token *parent, const ValueType &valueType, const V
10951095

10961096
static nonneg int getSizeOfType(const Token *typeTok, const Settings *settings)
10971097
{
1098-
const ValueType &valueType = ValueType::parseDecl(typeTok, *settings, true); // TODO: set isCpp
1098+
const ValueType &valueType = ValueType::parseDecl(typeTok, *settings);
10991099

11001100
return ValueFlow::getSizeOf(valueType, settings);
11011101
}
@@ -1298,7 +1298,7 @@ static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, b
12981298
setTokenValue(tok->next(), std::move(value), settings);
12991299
}
13001300
} else if (!tok2->type()) {
1301-
const ValueType& vt = ValueType::parseDecl(tok2, *settings, true); // TODO: set isCpp
1301+
const ValueType& vt = ValueType::parseDecl(tok2, *settings);
13021302
size_t sz = ValueFlow::getSizeOf(vt, settings);
13031303
const Token* brac = tok2->astParent();
13041304
while (Token::simpleMatch(brac, "[")) {
@@ -4765,7 +4765,7 @@ static bool isContainerOfPointers(const Token* tok, const Settings* settings)
47654765
return true;
47664766
}
47674767

4768-
ValueType vt = ValueType::parseDecl(tok, *settings, true); // TODO: set isCpp
4768+
ValueType vt = ValueType::parseDecl(tok, *settings);
47694769
return vt.pointer > 0;
47704770
}
47714771

@@ -8628,7 +8628,7 @@ static bool valueFlowIsSameContainerType(const ValueType& contType, const Token*
86288628
if (!tok || !tok->valueType() || !tok->valueType()->containerTypeToken)
86298629
return false;
86308630

8631-
const ValueType tokType = ValueType::parseDecl(tok->valueType()->containerTypeToken, *settings, true);
8631+
const ValueType tokType = ValueType::parseDecl(tok->valueType()->containerTypeToken, *settings);
86328632
return contType.isTypeEqual(&tokType);
86338633
}
86348634

@@ -8648,7 +8648,7 @@ static std::vector<ValueFlow::Value> getInitListSize(const Token* tok,
86488648
if (valueType->container->stdStringLike) {
86498649
initList = astIsGenericChar(args[0]) && !astIsPointer(args[0]);
86508650
} else if (containerTypeToken && settings) {
8651-
ValueType vt = ValueType::parseDecl(containerTypeToken, *settings, true); // TODO: set isCpp
8651+
ValueType vt = ValueType::parseDecl(containerTypeToken, *settings);
86528652
if (vt.pointer > 0 && astIsPointer(args[0]))
86538653
initList = true;
86548654
else if (vt.type == ValueType::ITERATOR && astIsIterator(args[0]))
@@ -9108,7 +9108,7 @@ static bool getMinMaxValues(const std::string &typestr, const Settings *settings
91089108
return false;
91099109
typeTokens.simplifyPlatformTypes();
91109110
typeTokens.simplifyStdType();
9111-
const ValueType &vt = ValueType::parseDecl(typeTokens.front(), *settings, true); // TODO: set isCpp
9111+
const ValueType &vt = ValueType::parseDecl(typeTokens.front(), *settings);
91129112
return getMinMaxValues(&vt, settings->platform, minvalue, maxvalue);
91139113
}
91149114

0 commit comments

Comments
 (0)