Skip to content

Commit baf5eaa

Browse files
TestValueFlow: Remove redundant test configuration, make settings const (#8776)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent b702ead commit baf5eaa

1 file changed

Lines changed: 17 additions & 28 deletions

File tree

test/testvalueflow.cpp

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,9 @@ class TestValueFlow : public TestFixture {
4343
TestValueFlow() : TestFixture("TestValueFlow") {}
4444

4545
private:
46-
/*const*/ Settings settings = settingsBuilder().library("std.cfg").build();
46+
const Settings settings = settingsBuilder().library("std.cfg").build();
4747

4848
void run() override {
49-
// strcpy, abort cfg
50-
constexpr char cfg[] = "<?xml version=\"1.0\"?>\n"
51-
"<def>\n"
52-
" <function name=\"strcpy\"> <arg nr=\"1\"><not-null/></arg> </function>\n"
53-
" <function name=\"abort\"> <noreturn>true</noreturn> </function>\n" // abort is a noreturn function
54-
"</def>";
55-
settings = settingsBuilder(settings).libraryxml(cfg).build();
5649

5750
mNewTemplate = true;
5851
TEST_CASE(valueFlowNumber);
@@ -437,9 +430,10 @@ class TestValueFlow : public TestFixture {
437430
return false;
438431
}
439432

440-
bool testValueOfX_(const char* file, int line, const char code[], unsigned int linenr, int value, ValueFlow::Value::ValueType type) {
433+
bool testValueOfX_(const char* file, int line, const char code[], unsigned int linenr, int value, ValueFlow::Value::ValueType type, const Settings* s = nullptr) {
434+
const Settings& curSettings = s ? *s : settings;
441435
// Tokenize..
442-
SimpleTokenizer tokenizer(settings, *this);
436+
SimpleTokenizer tokenizer(curSettings, *this);
443437
ASSERT_LOC(tokenizer.tokenize(code), file, line);
444438

445439
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) {
@@ -5887,21 +5881,19 @@ class TestValueFlow : public TestFixture {
58875881
ASSERT_EQUALS(false, value.isKnown());
58885882

58895883
// #13959
5890-
const Settings settingsOld = settings;
5891-
settings.standards.c = Standards::C23;
5884+
const Settings settingsC23 = settingsBuilder(settings).c(Standards::C23).build();
58925885
code = "void f(int* p) {\n"
58935886
" if (p == nullptr)\n"
58945887
" return;\n"
58955888
" if (p) {}\n"
58965889
"}\n";
5897-
value = valueOfTok(code, "p ) { }", &settings, /*cpp*/ false);
5890+
value = valueOfTok(code, "p ) { }", &settingsC23, /*cpp*/ false);
58985891
ASSERT_EQUALS(1, value.intvalue);
58995892
ASSERT_EQUALS(true, value.isKnown());
59005893

5901-
settings.standards.c = Standards::C17;
5902-
value = valueOfTok(code, "p ) { }", &settings, /*cpp*/ false);
5894+
const Settings settingsC17 = settingsBuilder(settings).c(Standards::C17).build();
5895+
value = valueOfTok(code, "p ) { }", &settingsC17, /*cpp*/ false);
59035896
ASSERT(value == ValueFlow::Value());
5904-
settings = settingsOld;
59055897
}
59065898

59075899
void valueFlowSizeofForwardDeclaredEnum() {
@@ -7880,56 +7872,55 @@ class TestValueFlow : public TestFixture {
78807872
void valueFlowDynamicBufferSize() {
78817873
const char *code;
78827874

7883-
const Settings settingsOld = settings; // TODO: get rid of this
7884-
settings = settingsBuilder(settings).library("posix.cfg").library("bsd.cfg").build();
7875+
const Settings settingsCfg = settingsBuilder(settings).library("posix.cfg").library("bsd.cfg").build();
78857876

78867877
code = "void* f() {\n"
78877878
" void* x = malloc(10);\n"
78887879
" return x;\n"
78897880
"}";
7890-
ASSERT_EQUALS(true, testValueOfX(code, 3U, 10, ValueFlow::Value::ValueType::BUFFER_SIZE));
7881+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 10, ValueFlow::Value::ValueType::BUFFER_SIZE, &settingsCfg));
78917882

78927883
code = "void* f() {\n"
78937884
" void* x = calloc(4, 5);\n"
78947885
" return x;\n"
78957886
"}";
7896-
ASSERT_EQUALS(true, testValueOfX(code, 3U, 20, ValueFlow::Value::ValueType::BUFFER_SIZE));
7887+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 20, ValueFlow::Value::ValueType::BUFFER_SIZE, &settingsCfg));
78977888

78987889
code = "void* f() {\n"
78997890
" const char* y = \"abcd\";\n"
79007891
" const char* x = strdup(y);\n"
79017892
" return x;\n"
79027893
"}";
7903-
ASSERT_EQUALS(true, testValueOfX(code, 4U, 5, ValueFlow::Value::ValueType::BUFFER_SIZE));
7894+
ASSERT_EQUALS(true, testValueOfX(code, 4U, 5, ValueFlow::Value::ValueType::BUFFER_SIZE, &settingsCfg));
79047895

79057896
code = "void* f() {\n"
79067897
" void* y = malloc(10);\n"
79077898
" void* x = realloc(y, 20);\n"
79087899
" return x;\n"
79097900
"}";
7910-
ASSERT_EQUALS(true, testValueOfX(code, 4U, 20, ValueFlow::Value::ValueType::BUFFER_SIZE));
7901+
ASSERT_EQUALS(true, testValueOfX(code, 4U, 20, ValueFlow::Value::ValueType::BUFFER_SIZE, &settingsCfg));
79117902

79127903
code = "void* f() {\n"
79137904
" void* y = calloc(10, 4);\n"
79147905
" void* x = reallocarray(y, 20, 5);\n"
79157906
" return x;\n"
79167907
"}";
7917-
ASSERT_EQUALS(true, testValueOfX(code, 4U, 100, ValueFlow::Value::ValueType::BUFFER_SIZE));
7908+
ASSERT_EQUALS(true, testValueOfX(code, 4U, 100, ValueFlow::Value::ValueType::BUFFER_SIZE, &settingsCfg));
79187909

79197910
code = "struct A {};\n" // #14305
79207911
"void* f() {\n"
79217912
" A* x = new A();\n"
79227913
" return x;\n"
79237914
"}";
7924-
ASSERT_EQUALS(true, testValueOfX(code, 4U, 1, ValueFlow::Value::ValueType::BUFFER_SIZE));
7915+
ASSERT_EQUALS(true, testValueOfX(code, 4U, 1, ValueFlow::Value::ValueType::BUFFER_SIZE, &settingsCfg));
79257916

79267917
code = "struct A {};\n"
79277918
"void* f() {\n"
79287919
" void* x = new A;\n"
79297920
" return x;\n"
79307921
"}";
79317922
{
7932-
auto values = tokenValues(code, "x ; }");
7923+
auto values = tokenValues(code, "x ; }", &settingsCfg);
79337924
ASSERT_EQUALS(1, values.size());
79347925
ASSERT(values.front().isSymbolicValue());
79357926
// TODO: add BUFFER_SIZE value = 1
@@ -7940,9 +7931,7 @@ class TestValueFlow : public TestFixture {
79407931
" B* x = new B();\n"
79417932
" return x;\n"
79427933
"}";
7943-
ASSERT_EQUALS(true, testValueOfX(code, 4U, 4, ValueFlow::Value::ValueType::BUFFER_SIZE));
7944-
7945-
settings = settingsOld;
7934+
ASSERT_EQUALS(true, testValueOfX(code, 4U, 4, ValueFlow::Value::ValueType::BUFFER_SIZE, &settingsCfg));
79467935
}
79477936

79487937
void valueFlowSafeFunctionParameterValues() {

0 commit comments

Comments
 (0)