From 1e6ea332e2cff8e47bbab3937e471f1c8f420508 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 28 Jul 2014 12:02:32 +0300 Subject: Add checks for public:/protected:/private: formatting. --- src/Makefile.am | 1 + src/rules/formatting.cpp | 86 ++++++++++++++++++++++++++++++++++++++ tests/testreport.txt | 9 ++++ tests/testsrc/bad/formatting.cpp | 51 ++++++++++++++++++++++ tests/testsrc/bad/formatting.h | 49 ++++++++++++++++++++++ tests/testsrc/bad/formatting2.cpp | 50 ++++++++++++++++++++++ tests/testsrc/good/formatting.cpp | 51 ++++++++++++++++++++++ tests/testsrc/good/formatting.h | 49 ++++++++++++++++++++++ tests/testsrc/good/formatting2.cpp | 50 ++++++++++++++++++++++ 9 files changed, 396 insertions(+) create mode 100644 src/rules/formatting.cpp create mode 100644 tests/testsrc/bad/formatting.cpp create mode 100644 tests/testsrc/bad/formatting.h create mode 100644 tests/testsrc/bad/formatting2.cpp create mode 100644 tests/testsrc/good/formatting.cpp create mode 100644 tests/testsrc/good/formatting.h create mode 100644 tests/testsrc/good/formatting2.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 1d16154..4cdfdfb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,6 +21,7 @@ mplint_SOURCES = \ rules/debug.cpp \ rules/dump.cpp \ rules/final.cpp \ + rules/formatting.cpp \ rules/include.cpp \ rules/license.cpp \ rules/po.cpp \ diff --git a/src/rules/formatting.cpp b/src/rules/formatting.cpp new file mode 100644 index 0000000..1a49848 --- /dev/null +++ b/src/rules/formatting.cpp @@ -0,0 +1,86 @@ +/* + * The ManaPlus Client + * Copyright (C) 2014 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "template.hpp" + +registerRuleExt(formatting, "011", "(.+)[.](cpp|h)") + +namespace +{ + unsigned int align = 0; + std::stack stack; +} // namespace + +startRule(formatting) +{ + align = 0; +} + +endRule(formatting) +{ +} + +parseLineRule(formatting) +{ + std::string data0 = data; + trim(data0); + if (data0 == "{") + { + stack.push(align); + align = data.size() - data0.size() + 4; +// print("open align=" + toString(align)); + } + else if (findCutFirst(data0, "{ ")) + { + const int sz = data0.size(); + trim(data0); + if (!data0.empty() && data0[0] != '/') + return; + stack.push(align); + align = data.size() - sz - 2 + 4; +// print("open align=" + toString(align)); + } + else if (data0 == "}" || data0 == "};" || findCutFirst(data0, "} ")) + { + if (stack.empty()) + return; + + align = stack.top(); + stack.pop(); +// print("close align=" + toString(align)); + } + else + { + std::string data2 = data; + if (findCutLast(data2, "public:") + || findCutLast(data2, "protected:") + || findCutLast(data2, "private:")) + { +// print("sz=" + toString(int(data2.size()))); +// print("align=" + toString(align)); + if (data2.size() != align) + { + print("Wrong public/protected/private formatting. " + "Must be align " + toString(align) + ", but present align " + + toString(static_cast(data2.size())) + "."); + } + } + } +} diff --git a/tests/testreport.txt b/tests/testreport.txt index 69b59bf..9a5df06 100644 --- a/tests/testreport.txt +++ b/tests/testreport.txt @@ -18,6 +18,13 @@ [testsrc/bad/final1.cpp:30]: V007: Need add final or notfinal into class declaration [testsrc/bad/final1.h:23]: V007: Need add final or notfinal into class declaration [testsrc/bad/final1.h:27]: V007: Need add final or notfinal into class declaration +[testsrc/bad/formatting2.cpp:28]: V011: Wrong public/protected/private formatting. Must be 4 space from previous level. +[testsrc/bad/formatting.cpp:27]: V011: Wrong public/protected/private formatting. Must be 4 space from previous level. +[testsrc/bad/formatting.cpp:35]: V011: Wrong public/protected/private formatting. Must be 4 space from previous level. +[testsrc/bad/formatting.cpp:45]: V011: Wrong public/protected/private formatting. Must be 4 space from previous level. +[testsrc/bad/formatting.h:25]: V011: Wrong public/protected/private formatting. Must be 4 space from previous level. +[testsrc/bad/formatting.h:33]: V011: Wrong public/protected/private formatting. Must be 4 space from previous level. +[testsrc/bad/formatting.h:43]: V011: Wrong public/protected/private formatting. Must be 4 space from previous level. [testsrc/bad/include1.cpp:21]: V008: Wrong include #include "lintmanager1.h". Probably you should use path from src dir, or use #include [testsrc/bad/include1.cpp:23]: V008: Wrong include #include "include1.h". Probably you should use path from src dir, or use #include [testsrc/bad/include1.h:21]: V008: Wrong include #include "lintmanager1.h". Probably you should use path from src dir, or use #include @@ -38,5 +45,7 @@ [testsrc/bad/uk.po:2253]: V010: Useless space at end of translation line. [testsrc/bad/uk.po:2257]: V010: Wrong character at end of translation line. [testsrc/bad/uk.po:44]: V010: Missing ### in translation. +[testsrc/bad/uk.po:496]: V010: Wrong character at end of translation line. [testsrc/bad/uk.po:919]: V010: Useless space at end of translation line. [testsrc/bad/uk.po:98]: V010: Useless space at end of translation line. +[testsrc/good/ru.po:1838]: V010: Wrong character at end of translation line. diff --git a/tests/testsrc/bad/formatting.cpp b/tests/testsrc/bad/formatting.cpp new file mode 100644 index 0000000..2f0bda5 --- /dev/null +++ b/tests/testsrc/bad/formatting.cpp @@ -0,0 +1,51 @@ +/* + * The ManaPlus Client + * Copyright (C) 2014 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "lintmanager.h" + +#include "debug.h" + +class Test1 final +{ +public: + Test1::Test1() + { + } +} + +struct Test2 final +{ +protected: + Test2::Test2() : + data1(), + data2() + { + } +} + +struct Test3 final +{ + private: + Test3::Test3() : + data1(), + data2() + { + } +} diff --git a/tests/testsrc/bad/formatting.h b/tests/testsrc/bad/formatting.h new file mode 100644 index 0000000..086e1f7 --- /dev/null +++ b/tests/testsrc/bad/formatting.h @@ -0,0 +1,49 @@ +/* + * The ManaPlus Client + * Copyright (C) 2014 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "lintmanager.h" + +class Test1 final +{ +public: + Test1::Test1() + { + } +} + +struct Test2 final +{ + protected: + Test2::Test2() : + data1(), + data2() + { + } +} + +struct Test3 final +{ + private: + Test3::Test3() : + data1(), + data2() + { + } +} diff --git a/tests/testsrc/bad/formatting2.cpp b/tests/testsrc/bad/formatting2.cpp new file mode 100644 index 0000000..ccf6e86 --- /dev/null +++ b/tests/testsrc/bad/formatting2.cpp @@ -0,0 +1,50 @@ +/* + * The ManaPlus Client + * Copyright (C) 2010 The Mana Developers + * Copyright (C) 2011-2014 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "debug.h" + +namespace +{ + static class SortPartyFunctor final + { + public: + bool operator() (const PartyMember *const p1, + const PartyMember *const p2) const + { + if (!p1 || !p2) + return false; + if (p1->getLeader()) + return true; + if (p2->getLeader()) + return false; + + if (p1->getName() != p2->getName()) + { + std::string s1 = p1->getName(); + std::string s2 = p2->getName(); + toLower(s1); + toLower(s2); + return s1 < s2; + } + return false; + } + } partySorter; +} // namespace diff --git a/tests/testsrc/good/formatting.cpp b/tests/testsrc/good/formatting.cpp new file mode 100644 index 0000000..2819d56 --- /dev/null +++ b/tests/testsrc/good/formatting.cpp @@ -0,0 +1,51 @@ +/* + * The ManaPlus Client + * Copyright (C) 2014 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "lintmanager.h" + +#include "debug.h" + +class Test1 final +{ + public: + Test1::Test1() + { + } +} + +struct Test2 final +{ + protected: + Test2::Test2() : + data1(), + data2() + { + } +} + +struct Test3 final +{ + private: + Test3::Test3() : + data1(), + data2() + { + } +} diff --git a/tests/testsrc/good/formatting.h b/tests/testsrc/good/formatting.h new file mode 100644 index 0000000..df123d2 --- /dev/null +++ b/tests/testsrc/good/formatting.h @@ -0,0 +1,49 @@ +/* + * The ManaPlus Client + * Copyright (C) 2014 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "lintmanager.h" + +class Test1 final +{ + public: + Test1::Test1() + { + } +} + +struct Test2 final +{ + protected: + Test2::Test2() : + data1(), + data2() + { + } +} + +struct Test3 final +{ + private: + Test3::Test3() : + data1(), + data2() + { + } +} diff --git a/tests/testsrc/good/formatting2.cpp b/tests/testsrc/good/formatting2.cpp new file mode 100644 index 0000000..e790ade --- /dev/null +++ b/tests/testsrc/good/formatting2.cpp @@ -0,0 +1,50 @@ +/* + * The ManaPlus Client + * Copyright (C) 2010 The Mana Developers + * Copyright (C) 2011-2014 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "debug.h" + +namespace +{ + static class SortPartyFunctor final + { + public: + bool operator() (const PartyMember *const p1, + const PartyMember *const p2) const + { + if (!p1 || !p2) + return false; + if (p1->getLeader()) + return true; + if (p2->getLeader()) + return false; + + if (p1->getName() != p2->getName()) + { + std::string s1 = p1->getName(); + std::string s2 = p2->getName(); + toLower(s1); + toLower(s2); + return s1 < s2; + } + return false; + } + } partySorter; +} // namespace -- cgit v1.2.3-60-g2f50