Skip to content

Commit

Permalink
Merge branch 'danmar:main' into feature/dump-suppression
Browse files Browse the repository at this point in the history
  • Loading branch information
wienans authored Feb 29, 2024
2 parents 5834d30 + f2e98f2 commit 53ede06
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ $(libcppdir)/checkuninitvar.o: lib/checkuninitvar.cpp lib/addoninfo.h lib/astuti
$(libcppdir)/checkunusedfunctions.o: lib/checkunusedfunctions.cpp externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/astutils.h lib/checkunusedfunctions.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkunusedfunctions.cpp

$(libcppdir)/checkunusedvar.o: lib/checkunusedvar.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/astutils.h lib/check.h lib/checkunusedvar.h lib/config.h lib/errortypes.h lib/fwdanalysis.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/vfvalue.h
$(libcppdir)/checkunusedvar.o: lib/checkunusedvar.cpp lib/addoninfo.h lib/astutils.h lib/check.h lib/checkunusedvar.h lib/config.h lib/errortypes.h lib/fwdanalysis.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkunusedvar.cpp

$(libcppdir)/checkvaarg.o: lib/checkvaarg.cpp lib/addoninfo.h lib/astutils.h lib/check.h lib/checkvaarg.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
Expand Down
43 changes: 0 additions & 43 deletions cfg/gnu.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,6 @@
<leak-ignore/>
<arg nr="1" direction="in"/>
</function>
<!-- int timerisset(struct timeval *tvp); -->
<function name="timerisset">
<noreturn>false</noreturn>
<leak-ignore/>
<use-retval/>
<returnValue type="int"/>
<arg nr="1" direction="in">
<not-uninit/>
<not-null/>
</arg>
</function>
<!-- void timerclear(struct timeval *tvp); -->
<function name="timerclear">
<noreturn>false</noreturn>
<leak-ignore/>
<returnValue type="void"/>
<arg nr="1" direction="inout">
<not-null/>
</arg>
</function>
<!-- https://man7.org/linux/man-pages/man3/getpw.3.html -->
<!-- int getpw(uid_t uid, char *buf); -->
<function name="getpw">
Expand All @@ -227,24 +207,6 @@
</arg>
<warn severity="style" reason="Obsolescent" alternatives="getpwnam"/>
</function>
<!-- void timeradd(struct timeval *a, struct timeval *b, struct timeval *res);-->
<!-- void timersub(struct timeval *a, struct timeval *b, struct timeval *res);-->
<function name="timeradd,timersub">
<noreturn>false</noreturn>
<leak-ignore/>
<returnValue type="void"/>
<arg nr="1" direction="in">
<not-uninit/>
<not-null/>
</arg>
<arg nr="2" direction="in">
<not-uninit/>
<not-null/>
</arg>
<arg nr="3" direction="out">
<not-null/>
</arg>
</function>
<!-- int __builtin_types_compatible_p (type1, type2) -->
<function name="__builtin_types_compatible_p">
<noreturn>false</noreturn>
Expand Down Expand Up @@ -1577,11 +1539,6 @@
<not-bool/>
</arg>
</function>
<!-- https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html -->
<!-- void __builtin_trap (void) -->
<function name="__builtin_trap">
<noreturn>true</noreturn>
</function>
<!-- int __builtin_constant_p (exp) -->
<function name="__builtin_constant_p">
<noreturn>false</noreturn>
Expand Down
44 changes: 43 additions & 1 deletion cfg/wxwidgets.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7135,6 +7135,15 @@
<returnValue type="wxString"/>
<warn severity="style">This function is deprecated, use 'wxPGProperty::GetValueAsString()' instead.</warn>
</function>
<!-- wxString wxPGPropertyFlags::wxPGProperty::GetValueAsStringWithCheck( wxPGPropValFormatFlags flags = wxPGPropValFormatFlags::Null ) const -->
<function name="wxPGPropertyFlags::wxPGProperty::GetValueAsStringWithCheck">
<noreturn>false</noreturn>
<leak-ignore/>
<returnValue type="wxString"/>
<const/>
<arg nr="1" default="wxPGPropValFormatFlags::Null" direction="in">
</arg>
</function>
<!-- wxString wxPlatformInfo::GetArchName ( ) const -->
<!-- Not possible to configure both functions:
static wxString wxPlatformInfo::GetArchName ( wxArchitecture arch) -->
Expand Down Expand Up @@ -15487,6 +15496,13 @@ wxItemKind kind = wxITEM_NORMAL) -->
<not-uninit/>
</arg>
</function>
<!-- void wxWindow::SetFocus( void ) -->
<!-- void wxSpinCtrl::SetFocus( void ) -->
<function name="wxWindow::SetFocus,wxSpinCtrl::SetFocus">
<noreturn>false</noreturn>
<leak-ignore/>
<returnValue type="void"/>
</function>
<!-- wxOperatingSystemId wxGetOsVersion(int * major = NULL, int * minor = NULL)-->
<function name="wxGetOsVersion">
<noreturn>false</noreturn>
Expand All @@ -15498,13 +15514,39 @@ wxItemKind kind = wxITEM_NORMAL) -->
</function>
<!-- int wxGrid::GetGridCursorRow() const -->
<!-- int wxGrid::GetGridCursorCol() const -->
<function name="wxGrid::GetGridCursorRow,wxGrid::GetGridCursorCol">
<!-- int wxKeyEvent::GetKeyCode (void) const -->
<function name="wxGrid::GetGridCursorRow,wxGrid::GetGridCursorCol,wxKeyEvent::GetKeyCode">
<noreturn>false</noreturn>
<returnValue type="int"/>
<use-retval/>
<leak-ignore/>
<const/>
</function>
<!-- bool wxColour::IsOk( void ) const -->
<function name="wxColour::IsOk">
<noreturn>false</noreturn>
<leak-ignore/>
<returnValue type="bool"/>
<use-retval/>
<const/>
</function>
<!-- const wxFont & wxDC::GetFont( void ) const -->
<function name="wxDC::GetFont">
<noreturn>false</noreturn>
<leak-ignore/>
<returnValue type="const wxFont &amp;"/>
<use-retval/>
<const/>
</function>
<!-- wxFont wxFont::Italic (void) const -->
<!-- wxFont wxFont::Larger (void) const -->
<function name="wxFont::Italic,wxFont::Larger">
<noreturn>false</noreturn>
<leak-ignore/>
<returnValue type="wxFont"/>
<use-retval/>
<const/>
</function>
<!-- wxColour wxGrid::GetGridLineColour() const -->
<function name="wxGrid::GetGridLineColour">
<noreturn>false</noreturn>
Expand Down
5 changes: 3 additions & 2 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2942,8 +2942,9 @@ static const Token* findExpressionChangedImpl(const Token* expr,
const Token* result = nullptr;
findAstNode(expr, [&](const Token* tok) {
if (exprDependsOnThis(tok)) {
result = findThisChanged(start, end, false, settings, cpp);
return true;
result = findThisChanged(start, end, /*indirect*/ 0, settings, cpp);
if (result)
return true;
}
bool global = false;
if (tok->variable()) {
Expand Down
11 changes: 2 additions & 9 deletions lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "errortypes.h"
#include "fwdanalysis.h"
#include "library.h"
#include "preprocessor.h"
#include "settings.h"
#include "symboldatabase.h"
#include "token.h"
Expand Down Expand Up @@ -1460,14 +1459,8 @@ void CheckUnusedVar::checkStructMemberUsage()
// Packed struct => possibly used by lowlevel code. Struct members might be required by hardware.
if (scope.bodyEnd->isAttributePacked())
continue;
if (const Preprocessor *preprocessor = mTokenizer->getPreprocessor()) {
const auto& directives = preprocessor->getDirectives();
const bool isPacked = std::any_of(directives.cbegin(), directives.cend(), [&](const Directive& d) {
return d.linenr < scope.bodyStart->linenr() && d.str == "#pragma pack(1)" && d.file == mTokenizer->list.getFiles().front();
});
if (isPacked)
continue;
}
if (mTokenizer->isPacked(scope.bodyStart))
continue;

// Bail out for template struct, members might be used in non-matching instantiations
if (scope.className.find('<') != std::string::npos)
Expand Down
5 changes: 3 additions & 2 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,10 @@ unsigned int CppCheck::checkClang(const std::string &path)
}

try {
std::istringstream ast(output2);
Tokenizer tokenizer(mSettings, this);
Preprocessor preprocessor(mSettings, this);
Tokenizer tokenizer(mSettings, this, &preprocessor);
tokenizer.list.appendFileIfNew(path);
std::istringstream ast(output2);
clangimport::parseClangAstDump(tokenizer, ast);
ValueFlow::setValues(tokenizer.list,
const_cast<SymbolDatabase&>(*tokenizer.getSymbolDatabase()),
Expand Down
8 changes: 5 additions & 3 deletions lib/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class CPPCHECKLIB Preprocessor {
// TODO: get rid of this
friend class PreprocessorHelper;
friend class TestPreprocessor;
friend class TestUnusedVar;

public:

Expand All @@ -93,9 +94,6 @@ class CPPCHECKLIB Preprocessor {
void inlineSuppressions(const simplecpp::TokenList &tokens, SuppressionList &suppressions);

void setDirectives(const simplecpp::TokenList &tokens);
void setDirectives(const std::list<Directive> &directives) {
mDirectives = directives;
}

/** list of all directives met while preprocessing file */
const std::list<Directive> &getDirectives() const {
Expand Down Expand Up @@ -183,6 +181,10 @@ class CPPCHECKLIB Preprocessor {

static bool hasErrors(const simplecpp::OutputList &outputList);

void setDirectives(const std::list<Directive> &directives) {
mDirectives = directives;
}

const Settings& mSettings;
ErrorLogger *mErrorLogger;

Expand Down
11 changes: 11 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10589,3 +10589,14 @@ bool Tokenizer::hasIfdef(const Token *start, const Token *end) const
d.file == list.getFiles()[start->fileIndex()];
});
}

bool Tokenizer::isPacked(const Token * bodyStart) const
{
assert(mPreprocessor);

const auto& directives = mPreprocessor->getDirectives();
// TODO: should this return true if the #pragma exists in any line before the start token?
return std::any_of(directives.cbegin(), directives.cend(), [&](const Directive& d) {
return d.linenr < bodyStart->linenr() && d.str == "#pragma pack(1)" && d.file == list.getFiles().front();
});
}
8 changes: 2 additions & 6 deletions lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "config.h"
#include "tokenlist.h"

#include <cassert>
#include <iosfwd>
#include <list>
#include <map>
Expand Down Expand Up @@ -367,13 +366,10 @@ class CPPCHECKLIB Tokenizer {
*/
static const Token * isFunctionHead(const Token *tok, const std::string &endsWith);

const Preprocessor *getPreprocessor() const {
assert(mPreprocessor);
return mPreprocessor;
}

bool hasIfdef(const Token *start, const Token *end) const;

bool isPacked(const Token * bodyStart) const;

private:

/** Simplify pointer to standard type (C only) */
Expand Down
13 changes: 12 additions & 1 deletion test/cli/clang-import_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,15 @@ def test_log(tmpdir):
'Checking {} ...'.format(test_file).replace('\\', '/'),
]

assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines)
assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines)


def test_warning(tmpdir): # #12424
test_file = os.path.join(tmpdir, 'test_2')
with open(test_file, 'wt') as f:
f.write('''void f() {}''')

exitcode, stdout, stderr = cppcheck(['-q', '--enable=warning', '--clang', test_file])
assert exitcode == 0, stderr # do not assert
assert stdout == ''
assert stderr == ''
19 changes: 19 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class TestCondition : public TestFixture {
TEST_CASE(incorrectLogicOperator14);
TEST_CASE(incorrectLogicOperator15);
TEST_CASE(incorrectLogicOperator16); // #10070
TEST_CASE(incorrectLogicOperator17);
TEST_CASE(secondAlwaysTrueFalseWhenFirstTrueError);
TEST_CASE(incorrectLogicOp_condSwapping);
TEST_CASE(testBug5895);
Expand Down Expand Up @@ -1722,6 +1723,24 @@ class TestCondition : public TestFixture {
ASSERT_EQUALS("", errout.str());
}

void incorrectLogicOperator17() { // #12471
check("struct R {\n"
" void set() { i = 1; }\n"
" int get() const { return i; }\n"
" int i;\n"
"};\n"
"struct P {\n"
" void f();\n"
" R* r;\n"
"};\n"
"void P::f() {\n"
" int a = r->get();\n"
" r->set();\n"
" if (a == 0 && r->get()) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

void secondAlwaysTrueFalseWhenFirstTrueError() {
check("void f(void) {\n" // #8892
" const char c[1] = { \'x\' }; \n"
Expand Down

0 comments on commit 53ede06

Please sign in to comment.