Skip to content

Commit aaff5ee

Browse files
committed
fixed usage of Standards::stdValue [skip ci]
1 parent ecb436a commit aaff5ee

5 files changed

Lines changed: 152 additions & 10 deletions

File tree

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ TESTOBJ = test/fixture.o \
343343
test/testsimplifyusing.o \
344344
test/testsingleexecutor.o \
345345
test/testsizeof.o \
346+
test/teststandards.o \
346347
test/teststl.o \
347348
test/teststring.o \
348349
test/testsummaries.o \
@@ -927,6 +928,9 @@ test/testsingleexecutor.o: test/testsingleexecutor.cpp cli/executor.h cli/single
927928
test/testsizeof.o: test/testsizeof.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/checksizeof.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h
928929
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testsizeof.cpp
929930

931+
test/teststandards.o: test/teststandards.cpp lib/addoninfo.h lib/check.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/standards.h lib/suppressions.h lib/utils.h test/fixture.h
932+
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/teststandards.cpp
933+
930934
test/teststl.o: test/teststl.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/checkstl.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
931935
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/teststl.cpp
932936

lib/cppcheck.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,10 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file)
446446
#endif
447447

448448
std::string flags(langOpt + " ");
449-
// TODO: does not apply C standard
450-
if (isCpp && !mSettings.standards.stdValue.empty())
451-
flags += "-std=" + mSettings.standards.stdValue + " ";
449+
if (isCpp && !mSettings.standards.stdValueCPP.empty())
450+
flags += "-std=" + mSettings.standards.stdValueCPP + " ";
451+
if (!isCpp && !mSettings.standards.stdValueC.empty())
452+
flags += "-std=" + mSettings.standards.stdValueC + " ";
452453

453454
for (const std::string &i: mSettings.includePaths)
454455
flags += "-I" + i + " ";

lib/standards.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,19 @@ struct Standards {
4444
enum cppstd_t : std::uint8_t { CPP03, CPP11, CPP14, CPP17, CPP20, CPP23, CPP26, CPPLatest = CPP26 } cpp = CPPLatest;
4545

4646
/** --std value given on command line */
47-
std::string stdValue;
47+
std::string stdValueC;
48+
49+
/** --std value given on command line */
50+
std::string stdValueCPP;
4851

4952
bool setC(std::string str) {
50-
stdValue = str;
51-
strTolower(str);
53+
if (str.empty())
54+
return false;
5255
c = getC(str);
53-
return !stdValue.empty() && str == getC();
56+
bool b = (str == getC());
57+
if (b)
58+
stdValueC = std::move(str);
59+
return b;
5460
}
5561
std::string getC() const {
5662
switch (c) {
@@ -86,10 +92,13 @@ struct Standards {
8692
return Standards::CLatest;
8793
}
8894
bool setCPP(std::string str) {
89-
stdValue = str;
90-
strTolower(str);
95+
if (str.empty())
96+
return false;
9197
cpp = getCPP(str);
92-
return !stdValue.empty() && str == getCPP();
98+
bool b = (str == getCPP());
99+
if (b)
100+
stdValueCPP = std::move(str);
101+
return b;
93102
}
94103
std::string getCPP() const {
95104
return getCPP(cpp);

test/testrunner.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<ClCompile Include="testsimplifyusing.cpp" />
9292
<ClCompile Include="testsingleexecutor.cpp" />
9393
<ClCompile Include="testsizeof.cpp" />
94+
<ClCompile Include="teststandards.cpp" />
9495
<ClCompile Include="teststl.cpp" />
9596
<ClCompile Include="teststring.cpp" />
9697
<ClCompile Include="testsummaries.cpp" />

test/teststandards.cpp

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2024 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include "fixture.h"
20+
#include "standards.h"
21+
22+
class TestStandards : public TestFixture {
23+
public:
24+
TestStandards() : TestFixture("TestStandards") {}
25+
26+
private:
27+
void run() override {
28+
TEST_CASE(set);
29+
TEST_CASE(setAlias);
30+
TEST_CASE(getC);
31+
TEST_CASE(getCPP);
32+
}
33+
34+
void set() const {
35+
Standards stds;
36+
ASSERT_EQUALS_ENUM(Standards::CLatest, stds.c);
37+
ASSERT_EQUALS("", stds.stdValueC);
38+
ASSERT_EQUALS_ENUM(Standards::CPPLatest, stds.cpp);
39+
ASSERT_EQUALS("", stds.stdValueCPP);
40+
41+
ASSERT_EQUALS(true, stds.setC("c99"));
42+
ASSERT_EQUALS_ENUM(Standards::C99, stds.c);
43+
ASSERT_EQUALS("c99", stds.stdValueC);
44+
ASSERT_EQUALS_ENUM(Standards::CPPLatest, stds.cpp);
45+
ASSERT_EQUALS("", stds.stdValueCPP);
46+
47+
ASSERT_EQUALS(true, stds.setC("c11"));
48+
ASSERT_EQUALS_ENUM(Standards::C11, stds.c);
49+
ASSERT_EQUALS("c11", stds.stdValueC);
50+
ASSERT_EQUALS_ENUM(Standards::CPPLatest, stds.cpp);
51+
ASSERT_EQUALS("", stds.stdValueCPP);
52+
53+
ASSERT_EQUALS(true, stds.setCPP("c++11"));
54+
ASSERT_EQUALS_ENUM(Standards::C11, stds.c);
55+
ASSERT_EQUALS("c11", stds.stdValueC);
56+
ASSERT_EQUALS_ENUM(Standards::CPP11, stds.cpp);
57+
ASSERT_EQUALS("c++11", stds.stdValueCPP);
58+
59+
ASSERT_EQUALS(true, stds.setCPP("c++23"));
60+
ASSERT_EQUALS_ENUM(Standards::C11, stds.c);
61+
ASSERT_EQUALS("c11", stds.stdValueC);
62+
ASSERT_EQUALS_ENUM(Standards::CPP23, stds.cpp);
63+
ASSERT_EQUALS("c++23", stds.stdValueCPP);
64+
65+
ASSERT_EQUALS(false, stds.setC("c77"));
66+
ASSERT_EQUALS_ENUM(Standards::C11, stds.c);
67+
ASSERT_EQUALS("c11", stds.stdValueC);
68+
ASSERT_EQUALS_ENUM(Standards::CPP23, stds.cpp);
69+
ASSERT_EQUALS("c++23", stds.stdValueCPP);
70+
71+
ASSERT_EQUALS(false, stds.setCPP("c+77"));
72+
ASSERT_EQUALS_ENUM(Standards::C11, stds.c);
73+
ASSERT_EQUALS("c11", stds.stdValueC);
74+
ASSERT_EQUALS_ENUM(Standards::CPP23, stds.cpp);
75+
ASSERT_EQUALS("c++23", stds.stdValueCPP);
76+
77+
ASSERT_EQUALS(false, stds.setC("C23"));
78+
ASSERT_EQUALS_ENUM(Standards::C11, stds.c);
79+
ASSERT_EQUALS("c11", stds.stdValueC);
80+
ASSERT_EQUALS_ENUM(Standards::CPP23, stds.cpp);
81+
ASSERT_EQUALS("c++23", stds.stdValueCPP);
82+
83+
ASSERT_EQUALS(false, stds.setCPP("C++11"));
84+
ASSERT_EQUALS_ENUM(Standards::C11, stds.c);
85+
ASSERT_EQUALS("c11", stds.stdValueC);
86+
ASSERT_EQUALS_ENUM(Standards::CPP23, stds.cpp);
87+
ASSERT_EQUALS("c++23", stds.stdValueCPP);
88+
}
89+
90+
void setAlias1() const {
91+
Standards stds;
92+
TODO_ASSERT_EQUALS(true, false, stds.setCPP("gnu++11"));
93+
ASSERT_EQUALS_ENUM(Standards::CLatest, stds.c);
94+
ASSERT_EQUALS("", stds.stdValueC);
95+
TODO_ASSERT_EQUALS_ENUM(Standards::CPP11, stds.cpp);
96+
TODO_ASSERT_EQUALS("gnu++11", "", stds.stdValueCPP);
97+
}
98+
99+
void setAlias2() const {
100+
Standards stds;
101+
TODO_ASSERT_EQUALS(true, false, stds.setC("gnu17"));
102+
TODO_ASSERT_EQUALS_ENUM(Standards::C17, stds.c);
103+
TODO_ASSERT_EQUALS("gnu17", "", stds.stdValueC);
104+
ASSERT_EQUALS_ENUM(Standards::CPPLatest, stds.cpp);
105+
ASSERT_EQUALS("", stds.stdValueCPP);
106+
}
107+
108+
void getC() const {
109+
ASSERT_EQUALS_ENUM(Standards::C99, Standards::getC("c99"));
110+
ASSERT_EQUALS_ENUM(Standards::C11, Standards::getC("c11"));
111+
112+
ASSERT_EQUALS_ENUM(Standards::CLatest, Standards::getC(""));
113+
ASSERT_EQUALS_ENUM(Standards::CLatest, Standards::getC("c77"));
114+
ASSERT_EQUALS_ENUM(Standards::CLatest, Standards::getC("C99"));
115+
}
116+
117+
void getCPP() const {
118+
ASSERT_EQUALS_ENUM(Standards::CPP11, Standards::getCPP("c++11"));
119+
ASSERT_EQUALS_ENUM(Standards::CPP23, Standards::getCPP("c++23"));
120+
121+
ASSERT_EQUALS_ENUM(Standards::CPPLatest, Standards::getCPP(""));
122+
ASSERT_EQUALS_ENUM(Standards::CPPLatest, Standards::getCPP("c++77"));
123+
ASSERT_EQUALS_ENUM(Standards::CPP11, Standards::getCPP("C++11"));
124+
}
125+
};
126+
127+
REGISTER_TEST(TestStandards)

0 commit comments

Comments
 (0)