From 42c3b3c89dec510870c8ff4195a410c4c4628d83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 8 Jun 2014 18:12:11 +0200 Subject: [PATCH] Library: Change range operator in validation pattern to ':' --- cfg/posix.cfg | 2 +- cfg/std.cfg | 74 ++++++++++++++++++++++---------------------- cfg/windows.cfg | 30 +++++++++--------- lib/library.cpp | 39 +++++++++++++---------- test/testlibrary.cpp | 17 ++++++++-- test/testother.cpp | 12 +++---- 6 files changed, 96 insertions(+), 78 deletions(-) diff --git a/cfg/posix.cfg b/cfg/posix.cfg index dfeae81e1..498c064d3 100644 --- a/cfg/posix.cfg +++ b/cfg/posix.cfg @@ -1,6 +1,6 @@ - false 0-999999 + false 0:999999 true false diff --git a/cfg/std.cfg b/cfg/std.cfg index 38c43ccbe..04f5403a5 100644 --- a/cfg/std.cfg +++ b/cfg/std.cfg @@ -32,7 +32,7 @@ false - 0- + 0: false @@ -42,7 +42,7 @@ false - 0- + 0: @@ -95,7 +95,7 @@ false - 0- + 0: @@ -115,18 +115,18 @@ false false false - false 0-255 - false 0-255 - false 0-255 - false 0-255 - false 0-255 - false 0-255 - false 0-255 - false 0-255 - false 0-255 - false 0-255 - false 0-255 - false 0-255 + false 0:255 + false 0:255 + false 0:255 + false 0:255 + false 0:255 + false 0:255 + false 0:255 + false 0:255 + false 0:255 + false 0:255 + false 0:255 + false 0:255 false false @@ -154,36 +154,36 @@ false - 0- - 0- + 0: + 0: false - 0- + 0: false - 0- + 0: false - 0- + 0: false - 0- + 0: false @@ -206,18 +206,18 @@ false - 0- + 0: false - 0- + 0: false - 0- + 0: false false @@ -260,25 +260,25 @@ false false false - false 0- - false 0- - false 0- + false 0: + false 0: + false 0: false false false false false - 0,2-36 - 0,2-36 - 0,2-36 - 0,2-36 - false 0-255 - false 0-255 - 0,2-36 - 0,2-36 - 0,2-36 - 0,2-36 + 0,2:36 + 0,2:36 + 0,2:36 + 0,2:36 + false 0:255 + false 0:255 + 0,2:36 + 0,2:36 + 0,2:36 + 0,2:36 false diff --git a/cfg/windows.cfg b/cfg/windows.cfg index 340e99282..f2319ea1b 100644 --- a/cfg/windows.cfg +++ b/cfg/windows.cfg @@ -185,41 +185,41 @@ - 0- + 0: false - 0- + 0: false - 0- + 0: false - 0- + 0: false - 0- + 0: false - 0- + 0: @@ -227,25 +227,25 @@ - 0- + 0: false - 0- + 0: false - 0- + 0: false - 0- + 0: @@ -253,13 +253,13 @@ - 0- + 0: false - 0- + 0: @@ -267,19 +267,19 @@ - 0- + 0: false - 0- + 0: false - 0- + 0: diff --git a/lib/library.cpp b/lib/library.cpp index da14983ac..aa7441bba 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -181,19 +181,24 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) else if (strcmp(argnode->Name(), "valid") == 0) { // Validate the validation expression const char *p = argnode->GetText(); - if (*p == '-') - ++p; - if (!std::isdigit(*p)) - return Error(BAD_ATTRIBUTE_VALUE, argnode->GetText()); + bool error = false; + bool range = false; for (; *p; p++) { if (std::isdigit(*p)) - continue; - if (*p == '-' && std::isdigit(*(p-1))) - continue; - if (*p == ',' && *(p+1) != ',') - continue; - return Error(BAD_ATTRIBUTE_VALUE, argnode->GetText()); + error |= (*(p+1) == '-'); + else if (*p == ':') + error |= range; + else if (*p == '-') + error |= (!std::isdigit(*(p+1))); + else if (*p == ',') + range = false; + else + error = true; + + range |= (*p == ':'); } + if (error) + return Error(BAD_ATTRIBUTE_VALUE, argnode->GetText()); // Set validation expression valid = argnode->GetText(); @@ -337,18 +342,20 @@ bool Library::isargvalid(const std::string &functionName, int argnr, const MathL TokenList tokenList(0); std::istringstream istr(ac->valid + ','); tokenList.createTokens(istr,""); - if (Token::Match(tokenList.front(), "- %num% -")) { - tokenList.front()->str("-" + tokenList.front()->strAt(1)); - tokenList.front()->deleteNext(); + for (Token *tok = tokenList.front(); tok; tok = tok->next()) { + if (Token::Match(tok,"- %num%")) { + tok->str("-" + tok->strAt(1)); + tok->deleteNext(); + } } for (const Token *tok = tokenList.front(); tok; tok = tok->next()) { if (tok->isNumber() && argvalue == MathLib::toLongNumber(tok->str())) return true; - if (Token::Match(tok, "%num% - %num%") && argvalue >= MathLib::toLongNumber(tok->str()) && argvalue <= MathLib::toLongNumber(tok->strAt(2))) + if (Token::Match(tok, "%num% : %num%") && argvalue >= MathLib::toLongNumber(tok->str()) && argvalue <= MathLib::toLongNumber(tok->strAt(2))) return true; - if (Token::Match(tok, "%num% - ,") && argvalue >= MathLib::toLongNumber(tok->str())) + if (Token::Match(tok, "%num% : ,") && argvalue >= MathLib::toLongNumber(tok->str())) return true; - if ((!tok->previous() || tok->previous()->str() == ",") && Token::Match(tok,"- %num%") && argvalue <= MathLib::toLongNumber(tok->strAt(1))) + if ((!tok->previous() || tok->previous()->str() == ",") && Token::Match(tok,": %num%") && argvalue <= MathLib::toLongNumber(tok->strAt(1))) return true; } return false; diff --git a/test/testlibrary.cpp b/test/testlibrary.cpp index f580f3368..4bb96b65a 100644 --- a/test/testlibrary.cpp +++ b/test/testlibrary.cpp @@ -112,9 +112,11 @@ private: const char xmldata[] = "\n" "\n" " \n" - " 1-\n" - " -7-0\n" - " 1-5,8\n" + " 1:\n" + " -7:0\n" + " 1:5,8\n" + " -1,5\n" + " :1,5\n" " \n" ""; tinyxml2::XMLDocument doc; @@ -145,6 +147,15 @@ private: ASSERT_EQUALS(false, library.isargvalid("foo", 3, 7)); ASSERT_EQUALS(true, library.isargvalid("foo", 3, 8)); ASSERT_EQUALS(false, library.isargvalid("foo", 3, 9)); + + // -1,5 + ASSERT_EQUALS(false, library.isargvalid("foo", 4, -10)); + ASSERT_EQUALS(true, library.isargvalid("foo", 4, -1)); + + // :1,5 + ASSERT_EQUALS(true, library.isargvalid("foo", 5, -10)); + ASSERT_EQUALS(true, library.isargvalid("foo", 5, 1)); + ASSERT_EQUALS(false, library.isargvalid("foo", 5, 2)); } void memory() const { diff --git a/test/testother.cpp b/test/testother.cpp index 40e640c12..ca6a915a4 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -219,7 +219,7 @@ private: if (posix) { const char cfg[] = "\n" "\n" - " 0-999999 \n" + " 0:999999 \n" ""; tinyxml2::XMLDocument xmldoc; xmldoc.Parse(cfg, sizeof(cfg)); @@ -768,7 +768,7 @@ private: const char cfg[] = "\n" "\n" " \n" - " 0,2-36 \n" + " 0,2:36 \n" ""; tinyxml2::XMLDocument xmldoc; xmldoc.Parse(cfg, sizeof(cfg)); @@ -794,10 +794,10 @@ private: TODO_ASSERT_EQUALS("error", "", errout.str()); invalidFunctionUsage("int f() { strtol(a,b,sizeof(a)!=12); }"); - ASSERT_EQUALS("[test.cpp:1]: (error) Invalid strtol() argument nr 3. The value is 0 or 1 (comparison result) but the valid values are '0,2-36'.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:1]: (error) Invalid strtol() argument nr 3. The value is 0 or 1 (comparison result) but the valid values are '0,2:36'.\n", errout.str()); invalidFunctionUsage("int f() { strtol(a,b,1); }"); - ASSERT_EQUALS("[test.cpp:1]: (error) Invalid strtol() argument nr 3. The value is 1 but the valid values are '0,2-36'.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:1]: (error) Invalid strtol() argument nr 3. The value is 1 but the valid values are '0,2:36'.\n", errout.str()); invalidFunctionUsage("int f() { strtol(a,b,10); }"); ASSERT_EQUALS("", errout.str()); @@ -7156,12 +7156,12 @@ private: check("void f(){\n" "usleep(1000000);\n" "}",nullptr,false,false,true); - ASSERT_EQUALS("[test.cpp:2]: (error) Invalid usleep() argument nr 1. The value is 1000000 but the valid values are '0-999999'.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (error) Invalid usleep() argument nr 1. The value is 1000000 but the valid values are '0:999999'.\n", errout.str()); check("void f(){\n" "usleep(1000001);\n" "}",nullptr,false,false,true); - ASSERT_EQUALS("[test.cpp:2]: (error) Invalid usleep() argument nr 1. The value is 1000001 but the valid values are '0-999999'.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (error) Invalid usleep() argument nr 1. The value is 1000001 but the valid values are '0:999999'.\n", errout.str()); } void checkCommaSeparatedReturn() {