|
21 | 21 | #include "checkcondition.h" |
22 | 22 | #include "testsuite.h" |
23 | 23 | #include <sstream> |
| 24 | +#include <tinyxml2.h> |
24 | 25 |
|
25 | 26 | extern std::ostringstream errout; |
26 | 27 |
|
@@ -319,8 +320,57 @@ class TestCondition : public TestFixture { |
319 | 320 | " else { if (x & 1); }\n" |
320 | 321 | "}"); |
321 | 322 | ASSERT_EQUALS("[test.cpp:4]: (style) Expression is always false because 'else if' condition matches previous condition at line 3.\n", errout.str()); |
| 323 | + |
| 324 | + check("extern int bar() __attribute__((pure));\n" |
| 325 | + "void foo(int x)\n" |
| 326 | + "{\n" |
| 327 | + " if ( bar() >1 && b) {}\n" |
| 328 | + " else if (bar() >1 && b) {}\n" |
| 329 | + "}"); |
| 330 | + ASSERT_EQUALS("[test.cpp:5]: (style) Expression is always false because 'else if' condition matches previous condition at line 4.\n", errout.str()); |
| 331 | + |
| 332 | + checkPureFunction("extern int bar();\n" |
| 333 | + "void foo(int x)\n" |
| 334 | + "{\n" |
| 335 | + " if ( bar() >1 && b) {}\n" |
| 336 | + " else if (bar() >1 && b) {}\n" |
| 337 | + "}"); |
| 338 | + ASSERT_EQUALS("[test.cpp:5]: (style) Expression is always false because 'else if' condition matches previous condition at line 4.\n", errout.str()); |
322 | 339 | } |
323 | 340 |
|
| 341 | + void checkPureFunction(const char code[]) { |
| 342 | + // Clear the error buffer.. |
| 343 | + errout.str(""); |
| 344 | + |
| 345 | + const char cfg[] = "<?xml version=\"1.0\"?>\n" |
| 346 | + "<def>\n" |
| 347 | + " <function name=\"bar\"> <pure/> </function>\n" |
| 348 | + "</def>"; |
| 349 | + tinyxml2::XMLDocument xmldoc; |
| 350 | + xmldoc.Parse(cfg, sizeof(cfg)); |
| 351 | + |
| 352 | + Settings settings; |
| 353 | + settings.addEnabled("style"); |
| 354 | + settings.addEnabled("warning"); |
| 355 | + settings.library.load(xmldoc); |
| 356 | + |
| 357 | + // Tokenize.. |
| 358 | + Tokenizer tokenizer(&settings, this); |
| 359 | + std::istringstream istr(code); |
| 360 | + tokenizer.tokenize(istr, "test.cpp"); |
| 361 | + |
| 362 | + CheckCondition checkCondition; |
| 363 | + checkCondition.runChecks(&tokenizer, &settings, this); |
| 364 | + const std::string str1(tokenizer.tokens()->stringifyList(0,true)); |
| 365 | + tokenizer.simplifyTokenList2(); |
| 366 | + const std::string str2(tokenizer.tokens()->stringifyList(0,true)); |
| 367 | + checkCondition.runSimplifiedChecks(&tokenizer, &settings, this); |
| 368 | + |
| 369 | + // Ensure that the test case is not bad. |
| 370 | + if (str1 != str2) { |
| 371 | + warnUnsimplified(str1, str2); |
| 372 | + } |
| 373 | + } |
324 | 374 | void duplicateIf() { |
325 | 375 | check("void f(int a, int &b) {\n" |
326 | 376 | " if (a) { b = 1; }\n" |
|
0 commit comments