diff --git a/script/build-bin-win32-dbg.cmd b/script/build-bin-win32-dbg.cmd index 0eb9512..2013c0e 100644 --- a/script/build-bin-win32-dbg.cmd +++ b/script/build-bin-win32-dbg.cmd @@ -1,6 +1,6 @@ @ECHO OFF -python --version > NUL +python --version > NUL IF "9009"=="%ERRORLEVEL%" (GOTO :NOPY) python cppnamelint.py chkenv @@ -12,9 +12,10 @@ ECHO ============================================================== ECHO Generate makefile via CMake ECHO ============================================================== SET ROOT_DIR=.. -SET BUILD_DIR=..\build +SET BUILD_DIR=..\build\windows +SET SOURCE_DIR=..\source SET BUILD_TYPE=DEBUG -::IF EXIST %BUILD_DIR% (RD %BUILD_DIR%) +python cppnamelint.py format %ROOT_DIR% %SOURCE_DIR% MKDIR %BUILD_DIR% python cppnamelint.py bldgcfg %ROOT_DIR% %BUILD_DIR% %BUILD_TYPE% IF NOT "0"=="%ERRORLEVEL%" (GOTO :PYERR) diff --git a/source/Detection.cpp b/source/Detection.cpp index a00441d..94f2860 100644 --- a/source/Detection.cpp +++ b/source/Detection.cpp @@ -204,8 +204,10 @@ bool Detection::_IsLowerSeperatedString(const string &Name, vector Ignor return bStatus; } -bool Detection::_IsHungarianNotationString(const string &TypeStr, const string &NameStr, - const bool &bPreferUpperCamel, const bool &bIsPtr, +bool Detection::_IsHungarianNotationString(const string &TypeStr, + const string &NameStr, + const bool &bPreferUpperCamel, + const bool &bIsPtr, const bool &bIsArray, const vector &IgnorePrefixs, const map &TypeNamingMap, @@ -233,9 +235,15 @@ bool Detection::_IsHungarianNotationString(const string &TypeStr, const string & // Remove pointer chars. // Remove hungarian prefix chars. // - bool bSzStringMatched = false; + bool bStatus = false; if (bIsPtr || bIsArray) { - size_t nLowerPCount = this->_RemoveHeadingPtrChar(NewNameStr); + // Remove `p` chars. + size_t nLowerPCount = this->_RemoveHeadingPtrChar(NewNameStr); + if (nLowerPCount > 0) { + bStatus = true; + } + + // Remove hungarian notion of C null string. for (auto Iter : NullStringMap) { const auto IterType = Iter.Key; const auto IterPrefix = Iter.Value; @@ -245,42 +253,44 @@ bool Detection::_IsHungarianNotationString(const string &TypeStr, const string & if (0 == nNewStrPos) { const size_t nPrefixLen = IterPrefix.length(); - bSzStringMatched = true; + bStatus = true; NewNameStr = NewNameStr.substr(nPrefixLen, NewNameStr.length() - nPrefixLen); break; } } } } + else + { + bStatus = true; + } - bool bStatus = true; - bool bSpecificTypeMatched = false; - if (!bSzStringMatched) { - for (map::const_iterator Iter = TypeNamingMap.begin(); - Iter != TypeNamingMap.end(); Iter++) { - const auto IterType = Iter->first; - const auto IterPrefix = Iter->second; - if (IterType == NewTypeStr) { - bStatus = false; - const size_t nNewStrPos = NewNameStr.find(IterPrefix); - if (0 == nNewStrPos) { - const size_t nPrefixLen = IterPrefix.length(); + // Match Hungarian notation. + if (bStatus) + { + for (map::const_iterator Iter = TypeNamingMap.begin(); + Iter != TypeNamingMap.end(); Iter++) { - bStatus = true; - bSpecificTypeMatched = true; - NewNameStr = NewNameStr.substr(nPrefixLen, NewNameStr.length() - nPrefixLen); - break; - } - } - } + const auto IterType = Iter->first; + const auto IterPrefix = Iter->second; + + if (IterType == NewTypeStr) { + const size_t nNewStrPos = NewNameStr.find(IterPrefix); + bStatus = (0 == nNewStrPos); + //if (bStatus) { + // const size_t nPrefixLen = IterPrefix.length(); + + // // Remove hungarian notation. + // NewNameStr = NewNameStr.substr(nPrefixLen, NewNameStr.length() - nPrefixLen); + //} + + break; + } + } } - if (!bSpecificTypeMatched) { - if (bPreferUpperCamel) { - bStatus = this->_IsUpperCamelCaseString(NewNameStr, IgnorePrefixs); - } else { - bStatus = false; - } + if (bPreferUpperCamel) { + bStatus = this->_IsUpperCamelCaseString(NewNameStr, IgnorePrefixs); } return bStatus; diff --git a/source/test/TestDetection.cpp b/source/test/TestDetection.cpp index 0279388..74969f2 100644 --- a/source/test/TestDetection.cpp +++ b/source/test/TestDetection.cpp @@ -292,9 +292,9 @@ TEST(Config_Detect_CheckVariable, InputParms_Good) { RuleOfVariable Rule; Detection Detect; - Rule.WordListMap.insert(std::pair("int" , "i")); - Rule.WordListMap.insert(std::pair("uin8_t" , "u8")); - Rule.WordListMap.insert(std::pair("uin16_t", "u16")); + Rule.WordListMap.insert(std::pair("int" , "i")); + Rule.WordListMap.insert(std::pair("uint8_t" , "u8")); + Rule.WordListMap.insert(std::pair("uint16_t", "u16")); Rule.NullStringMap.push_back(MappingPair("char*" , "sz")); Rule.NullStringMap.push_back(MappingPair("char**" , "psz")); @@ -304,46 +304,54 @@ TEST(Config_Detect_CheckVariable, InputParms_Good) Rule.NullStringMap.push_back(MappingPair("char[]" , "sz")); Rule.NullStringMap.push_back(MappingPair("wchar_t[]" , "wsz")); - bool bPrefer = true; - bool bIsPtr = false; - bool bIsArray = false; + bool bPrefer = true; + bool bIsPtr = true; + bool bNotPtr = false; + bool bIsArray = true; + bool bNotArray = false; Detect.ApplyRuleForVariable(Rule); // ...........................................................vvvvvvvvv <-- * character should be removed. - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "char" , "szStr" , bPrefer, true , bIsArray)); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "char" , "pszStr" , bPrefer, true , bIsArray)); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "wchar_t" , "wszStr" , bPrefer, true , bIsArray)); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "wchar_t" , "pwszStr" , bPrefer, true , bIsArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "char" , "szStr" , bPrefer, bIsPtr, bNotArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "char" , "pszStr" , bPrefer, bIsPtr, bNotArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "wchar_t" , "wszStr" , bPrefer, bIsPtr, bNotArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "wchar_t" , "pwszStr" , bPrefer, bIsPtr, bNotArray)); // ...........................................................vvvvvvvvv <-- [] characters should be removed. - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "char" , "szStr" , bPrefer, false , true)); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "wchar_t" , "wszStr" , bPrefer, false , true)); - - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "piMyValue" , bPrefer, true , bIsArray)); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_DEFAULT , "uin8_t" , "MyFunc" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_UPPER_CAMEL , "uin8_t" , "MyFunc" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uin8_t" , "myFunc" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_SNAKE , "uin8_t" , "my_func" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "iMyFunc" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin8_t" , "u8MyFunc" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin16_t" , "u16MyFunc" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "char" , "szStr" , bPrefer, bNotPtr, bIsArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "wchar_t" , "wszStr" , bPrefer, bNotPtr, bIsArray)); + + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "piMyValue" , bPrefer, bIsPtr, bNotArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_DEFAULT , "uint8_t" , "MyFunc" , bPrefer, bNotPtr, bNotArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_UPPER_CAMEL , "uint8_t" , "MyFunc" , bPrefer, bNotPtr, bNotArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uint8_t" , "myFunc" , bPrefer, bNotPtr, bNotArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_SNAKE , "uint8_t" , "my_func" , bPrefer, bNotPtr, bNotArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "iMyFunc" , bPrefer, bNotPtr, bNotArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uint8_t" , "u8MyFunc" , bPrefer, bNotPtr, bNotArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uint16_t" , "u16MyFunc" , bPrefer, bNotPtr, bNotArray)); Rule.IgnorePrefixs.push_back("m_"); Detect.ApplyRuleForVariable(Rule); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "m_iMyFunc" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "m_iMyFunc" , bPrefer, bNotPtr, bNotArray)); } TEST(Config_Detect_CheckVariable, PreferUpperCamelIfMissed) { RuleOfVariable Rule; Detection Detect; - + + bool bIsPtr = true; + bool bNotPtr = false; + bool bIsArray = true; + bool bNotArray = false; bool bPreferUpperCamel = true; - Rule.WordListMap.insert(std::pair("uin8_t", "u8")); - Rule.WordListMap.insert(std::pair("uin16_t", "u16")); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN, "int", "MyValue", bPreferUpperCamel, false, false)); - EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN, "uin8_t", "MyValue", bPreferUpperCamel, false, false)); + Rule.WordListMap.insert(std::pair("uint8_t", "u8")); + Rule.WordListMap.insert(std::pair("uint16_t", "u16")); + Detect.ApplyRuleForVariable(Rule); + + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN, "int", "MyValue", bPreferUpperCamel, bNotPtr, bNotArray)); + EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN, "uint8_t", "MyValue", bPreferUpperCamel, bNotPtr, bNotArray)); } TEST(Config_Detect_CheckVariable, InputParms_Bad) @@ -351,27 +359,26 @@ TEST(Config_Detect_CheckVariable, InputParms_Bad) RuleOfVariable Rule; Detection Detect; - Rule.WordListMap.insert(std::pair("uin8_t", "u8")); + Rule.WordListMap.insert(std::pair("uint8_t", "u8")); Rule.WordListMap.insert(std::pair("uin16_t", "u16")); bool bPrefer = true; bool bIsPtr = false; bool bIsArray = false; Detect.ApplyRuleForVariable(Rule); - EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_DEFAULT , "uin8_t" , "" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_UPPER_CAMEL , "uin8_t" , "" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uin8_t" , "" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_LOWER_SNAKE , "uin8_t" , "" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uin8_t" , "" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin8_t" , "u8my_name" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin8_t" , "u8my_Name" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin8_t" , "u8my_Name" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin8_t" , "u16AnyName" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin16_t" , "u8AnyName" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin8_t" , "u8_my_func" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin16_t" , "u16_my_func" , bPrefer, bIsPtr, bIsArray)); - - EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "iMyValue" , bPrefer, true, bIsArray)); + EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_DEFAULT , "uint8_t" , "" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_UPPER_CAMEL , "uint8_t" , "" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uint8_t" , "" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_LOWER_SNAKE , "uint8_t" , "" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uint8_t" , "" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uint8_t" , "u8my_name" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uint8_t" , "u8my_Name" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uint8_t" , "u8my_Name" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uint8_t" , "u16AnyName" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin16_t" , "u8AnyName" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uint8_t" , "u8_my_func" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin16_t" , "u16_my_func" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "iMyValue" , bPrefer, true, bIsArray)); } //------------------------------------------------------------------------- @@ -383,16 +390,16 @@ TEST(Config_Detect_CheckVariable, GoodCases) RuleOfVariable Rule; Detection Detect; - Rule.WordListMap.insert(std::pair("uin8_t", "u8")); + Rule.WordListMap.insert(std::pair("uint8_t", "u8")); bool bPrefer = true; bool bIsPtr = false; bool bIsArray = false; Detect.ApplyRuleForVariable(Rule); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_DEFAULT , "uin8_t", "MyFunc" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_UPPER_CAMEL , "uin8_t", "MyFunc" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uin8_t", "myFunc" , bPrefer, bIsPtr, bIsArray)); - EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_SNAKE , "uin8_t", "my_func" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_DEFAULT , "uint8_t", "MyFunc" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_UPPER_CAMEL , "uint8_t", "MyFunc" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uint8_t", "myFunc" , bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_SNAKE , "uint8_t", "my_func" , bPrefer, bIsPtr, bIsArray)); } TEST(Config_Detect_CheckVariable, Hungarian_Bad) @@ -400,13 +407,13 @@ TEST(Config_Detect_CheckVariable, Hungarian_Bad) RuleOfVariable Rule; Detection Detect; - Rule.WordListMap.insert(std::pair("uin8_t", "u8")); + Rule.WordListMap.insert(std::pair("uint8_t", "u8")); bool bPrefer = true; bool bIsPtr = false; bool bIsArray = false; Detect.ApplyRuleForVariable(Rule); - EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN, "uin8_t", "MyFunc", bPrefer, bIsPtr, bIsArray)); + EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN, "uint8_t", "MyFunc", bPrefer, bIsPtr, bIsArray)); } } // namespace CheckVariable