cppcheck/test
Rikard Falkeborn d7a8e25d92
Fix #9647: Set correct enum value (#2856)
* Tokenize: Set varId for variables in enum

Set varIds in enum values. It was previously disabled in 5119ae84b8
to avoid issues with enums named the same as global variables. Take care
to only set varids to variables used to set the value of an enumerator,
not the enumerator itself. This is somewhat complicated by the fact that
at the time this happens, astOperand1(), astOperand2(), astParent() etc
are not set. The current implementation is not perfect, for example in
the code below, y will not have a varid set, but x and z will. This is
deemed sufficient for now.

            int x, y, z;
            enum E { a = f(x, y, z); };

* Fix #9647: Value of enums with variables as init values

C++ allows enum values to be set using constexprs, which cppcheck did
not handle before. To solve this, add a new pass to valueflow to update
enum values after global consts have been processed. In order to do so,
I moved all settings of enum values to valueflow. After setting the enum
values, we need another call to valueFlowNumber() to actually set users
of the enums.

There is still room for improvements, since each pass of
valueFlowGlobalConstVar() and valueFlowEnumValue() only sets variables
that are possible to set directly, and not if setting the value of a
variable allows us to set the value of another. For example

	constexpr int a = 5;
	constexpr int b = a + 5;
	enum E { X = a };
	constexpr E e = X;

Here both b and e will not have their values set, even though cppcheck
should be possible to figure out their values. That's for another PR
though.

This was tested by running test-my-pr.py with 500 packages. The only
difference was one error message in fairy-stockfish_11.1, where cppcheck
now printed the correct size of an array instead of 2147483648 which I
assume is some kind of default value. In that package, using a constexpr
when setting enum values is common, but as mentioned, there was no
change in the number of warnings.
2020-10-22 07:45:04 +02:00
..
bug-hunting
cfg Astyle formatting code [ci skip] 2020-10-01 08:33:16 +02:00
cli Clang import; fixed ast for function call with multiple arguments 2020-10-09 07:54:16 +02:00
synthetic
testsuites
CMakeLists.txt disabled precompiled headers in CMake as they currently do not emit any compiler warnings / adjusted check for precompiled header (#2820) 2020-09-23 07:51:21 +02:00
options.cpp
options.h
precompiled.h
redirect.h
test.cxx
test64bit.cpp
testassert.cpp Fix crash in cbmc detected with daca@home 2020-09-24 20:48:26 +02:00
testastutils.cpp
testautovariables.cpp Fix issue 9939: False positive: Reference to temporary returned (static variable) (#2840) 2020-10-06 09:16:54 +02:00
testbool.cpp Update Copyright year 2020-10-03 09:15:56 +02:00
testboost.cpp
testbufferoverrun.cpp
testbughuntingchecks.cpp Bug hunting: Avoid uninit struct member false positives 2020-09-09 18:26:04 +02:00
testcharvar.cpp
testclangimport.cpp Fix TestClangImport 2020-10-06 23:19:32 +02:00
testclass.cpp Fixed #9821 (False positive: Delegating constructor and initialization list) 2020-09-09 18:04:21 +02:00
testcmdlineparser.cpp
testcondition.cpp Crash: Add regression test 2020-10-22 07:40:06 +02:00
testconstructors.cpp Clarify inconclusive uninitMemberVar warning in copy constructors, it's in general a good idea to copy all the data. 2020-08-05 13:57:40 +02:00
testcppcheck.cpp
testerrorlogger.cpp
testexceptionsafety.cpp
testexprengine.cpp Bug hunting; Better handling of constant 2020-10-08 17:24:35 +02:00
testfilelister.cpp
testfunctions.cpp
testgarbage.cpp
testimportproject.cpp ImportProject: Add test case 2020-09-06 16:08:42 +02:00
testincompletestatement.cpp Avoid constStatement false positives for 'foo() || x=5'. Found in daca@home. 2020-09-29 13:35:39 +02:00
testinternal.cpp
testio.cpp
testleakautovar.cpp
testlibrary.cpp
testmathlib.cpp
testmemleak.cpp Fix #9228 (FN common realloc mistake with assignment of NULL) 2020-09-13 21:49:49 +02:00
testnullpointer.cpp Fix issue 9842: ValueFlow: wrong handling of ?, seems to think that the whole expression is a condition (#2821) 2020-09-23 07:45:03 +02:00
testoptions.cpp
testother.cpp Fixed #9929 (False positive: suspicious semicolon after macro) 2020-10-09 20:34:20 +02:00
testpath.cpp
testpathmatch.cpp
testplatform.cpp
testpostfixoperator.cpp
testpreprocessor.cpp
testrunner.cpp
testrunner.vcxproj
testrunner.vcxproj.filters
testsamples.cpp
testsimplifytemplate.cpp fix #9886 (Hang in TemplateSimplifier (gcc/gcc/testsuite/g++.dg/cpp0x/decltype34.C)) 2020-09-10 14:47:59 -04:00
testsimplifytokens.cpp
testsimplifytypedef.cpp Clang import testing: Compare AST 2020-10-04 11:27:31 +02:00
testsimplifyusing.cpp
testsizeof.cpp
teststl.cpp Fix issue 9907: False positive: knownEmptyContainer after function call with :: (#2814) 2020-09-20 22:37:28 +02:00
teststring.cpp
testsuite.cpp
testsuite.h
testsuppressions.cpp Add support for comments at end of suppression in suppression files (#2736) 2020-08-20 21:49:07 +02:00
testsymboldatabase.cpp Fix #9647: Set correct enum value (#2856) 2020-10-22 07:45:04 +02:00
testthreadexecutor.cpp
testtimer.cpp
testtoken.cpp Fixed #8506 (CPPCheck printing invalid characters in output) 2020-09-04 20:43:54 +02:00
testtokenize.cpp Clang import testing: Compare AST 2020-10-04 11:27:31 +02:00
testtokenlist.cpp
testtype.cpp
testuninitvar.cpp Fix issue 9855: false positive: uninitvar (#2754) 2020-08-26 07:02:15 +02:00
testunusedfunctions.cpp
testunusedprivfunc.cpp
testunusedvar.cpp Fixed #9707 (False positive: unreadVariable, union) 2020-10-02 20:22:22 +02:00
testutils.cpp
testutils.h
testvaarg.cpp
testvalueflow.cpp Fix issue 9945: FP: containerOutOfBounds (#2845) 2020-10-22 07:41:52 +02:00
testvarid.cpp Fix #9647: Set correct enum value (#2856) 2020-10-22 07:45:04 +02:00