xaizek / zograscope (License: AGPLv3 only) (since 2018-12-07)
Mainly a syntax-aware diff that also provides a number of additional tools.
<root> / tests / srcml / cxx / srcml-cxx-parser.cpp (0cbabe21f325418cc11dab27f835c2505742fbfc) (13KiB) (mode 100644) [raw]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
// Copyright (C) 2017 xaizek <xaizek@posteo.net>
//
// This file is part of zograscope.
//
// zograscope is free software: you can redistribute it and/or modify
// it under the terms of version 3 of the GNU Affero General Public License as
// published by the Free Software Foundation.
//
// zograscope 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with zograscope.  If not, see <http://www.gnu.org/licenses/>.

// These are tests of basic properties of a parser.  Things like:
//  - whether some constructs can be parsed
//  - whether elements are identified correctly

#include "Catch/catch.hpp"

#include <fstream>
#include <memory>

#include "srcml/cxx/SrcmlCxxSType.hpp"
#include "utils/fs.hpp"
#include "utils/strings.hpp"
#include "TermHighlighter.hpp"
#include "TreeBuilder.hpp"
#include "tree.hpp"

#include "pmr/monolithic.hpp"

#include "tests.hpp"

using namespace srcmlcxx;

static const auto makePred = [](Type type, std::string label) {
    return [=](const Node *node) {
        return (node->type == type && node->label == label);
    };
};

TEST_CASE("Position of tokens is computed correctly",
          "[.srcml][srcml-cxx][parser]")
{
    // The problem here was common prefix of two adjacent tokens: `auto` and
    // `a`.
    std::string text = R"(auto a = "separator";)";

    Tree tree = parseCxx(text);
    std::string output = TermHighlighter(tree).print();
    CHECK(output == text);
}

TEST_CASE("Literals are marked with types", "[.srcml][srcml-cxx][parser]")
{
    Tree tree = parseCxx(R"(
        auto a = true || false;
        auto b = 'a' + L'a';
        auto c = "a" L"a";
        auto d = nullptr;
        auto e = 10 + 0x1 + 001;
        auto f = 10i+1;
    )");
    CHECK(findNode(tree, makePred(Type::IntConstants, "true")) != nullptr);
    CHECK(findNode(tree, makePred(Type::IntConstants, "false")) != nullptr);
    CHECK(findNode(tree, makePred(Type::CharConstants, "'a'")) != nullptr);
    CHECK(findNode(tree, makePred(Type::StrConstants, R"("a")")) != nullptr);
    CHECK(findNode(tree, makePred(Type::StrConstants, R"(L"a")")) != nullptr);
    CHECK(findNode(tree, makePred(Type::IntConstants, "nullptr")) != nullptr);
    CHECK(findNode(tree, makePred(Type::IntConstants, "10")) != nullptr);
    CHECK(findNode(tree, makePred(Type::IntConstants, "0x1")) != nullptr);
    CHECK(findNode(tree, makePred(Type::IntConstants, "001")) != nullptr);
    CHECK(findNode(tree, makePred(Type::FPConstants, "10i+1")) != nullptr);
}

TEST_CASE("Operators are marked with types", "[.srcml][srcml-cxx][parser]")
{
    Tree tree = parseCxx(R"(
        void f() {
            a += 1;
        }
    )");
    CHECK(findNode(tree, makePred(Type::Operators, "+=")) != nullptr);
}

TEST_CASE("Types are marked with types", "[.srcml][srcml-cxx][parser]")
{
    Tree tree = parseCxx(R"(
        void f() {
            User a;
        }
    )");
    CHECK(findNode(tree, makePred(Type::Keywords, "void")) != nullptr);
    CHECK(findNode(tree, makePred(Type::UserTypes, "User")) != nullptr);
}

TEST_CASE("Specifiers are marked with types", "[.srcml][srcml-cxx][parser]")
{
    Tree tree = parseCxx("const int a;");
    CHECK(findNode(tree, makePred(Type::Specifiers, "const")) != nullptr);
}

TEST_CASE("Brackets are marked with types", "[.srcml][srcml-cxx][parser]")
{
    Tree tree;

    tree = parseCxx(R"(
        void f(int arg) {
            int a[arg];
        }
    )");
    CHECK(findNode(tree, makePred(Type::LeftBrackets, "(")) != nullptr);
    CHECK(findNode(tree, makePred(Type::LeftBrackets, "{")) != nullptr);
    CHECK(findNode(tree, makePred(Type::LeftBrackets, "[")) != nullptr);
    CHECK(findNode(tree, makePred(Type::RightBrackets, ")")) != nullptr);
    CHECK(findNode(tree, makePred(Type::RightBrackets, "}")) != nullptr);
    CHECK(findNode(tree, makePred(Type::RightBrackets, "]")) != nullptr);

    tree = parseCxx(R"(
        int a = (1 + 2);
    )");
    CHECK(findNode(tree, makePred(Type::LeftBrackets, "(")) != nullptr);
    CHECK(findNode(tree, makePred(Type::RightBrackets, ")")) != nullptr);
}

TEST_CASE("Keywords are marked with types", "[.srcml][srcml-cxx][parser]")
{
    Tree tree = parseCxx(R"(
        void f() {
            if (0) {
                return  ;
            } else {
            }
            switch (1) {
                default:break;
            }
        }
    )");
    CHECK(findNode(tree, makePred(Type::Keywords, "if")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Keywords, "return")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Keywords, "else")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Keywords, "switch")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Keywords, "default")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Keywords, "break")) != nullptr);

    tree = parseCxx(R"(
        void f() {
            if (0) {
            } else if (this) {
            }
        }
    )");
    CHECK(findNode(tree, makePred(Type::Keywords, "else")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Keywords, "this")) != nullptr);
    auto test = [](const Node *node) {
        return node->label == "if"
            && node->type == Type::Keywords
            && node->line == 4;
    };
    CHECK(findNode(tree, test) != nullptr);
}

TEST_CASE("this is recognized as a keyword", "[.srcml][srcml-cxx][parser]")
{
    Tree tree = parseCxx(R"(
        void Class::f() {
            this->a = 10;
        }
    )");
    CHECK(findNode(tree, makePred(Type::Keywords, "this")) != nullptr);
}

TEST_CASE("Function names are marked with types", "[.srcml][srcml-cxx][parser]")
{
    Tree tree = parseCxx(R"(
        void func() {
            call();
            obj.as<int>();
        }
        void Class::method() {
            obj.meth();
        }
    )");
    CHECK(findNode(tree, makePred(Type::Functions, "func")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Functions, "call")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Functions, "as")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Functions, "Class")) == nullptr);
    CHECK(findNode(tree, makePred(Type::Functions, "method")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Functions, "meth")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Functions, "obj")) == nullptr);
}

TEST_CASE("Comments are marked with types", "[.srcml][srcml-cxx][parser]")
{
    Tree tree = parseCxx(R"(
        /* mlcom */
        // slcom
    )");
    CHECK(findNode(tree, makePred(Type::Comments, "/* mlcom */")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Comments, "// slcom")) != nullptr);
}

TEST_CASE("Directives are marked with types", "[.srcml][srcml-cxx][parser]")
{
    Tree tree = parseCxx(R"(
        #include <something>
        #if 0
        #include "something"
        #elif defined(something)
        #endif // com
        #define macro(x) (x+x) /* that */ x
    )");
    CHECK(findNode(tree, makePred(Type::Directives, "#")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Directives, "include")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Directives, "if")) != nullptr);
    CHECK(findNode(tree, makePred(Type::IntConstants, "0")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Comments, "// com")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Comments, "/* that */")) != nullptr);
}

TEST_CASE("Identifiers are marked with types", "[.srcml][srcml-cxx][parser]")
{
    Tree tree = parseCxx(R"(
        void Class::method() {
            int var;
        }
    )");
    CHECK(findNode(tree, makePred(Type::Identifiers, "Class")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Identifiers, "var")) != nullptr);
}

TEST_CASE("Block nodes are spliced into their parents",
          "[.srcml][srcml-cxx][parser]")
{
    Tree tree = parseCxx(R"(
        struct Struct {
            int callNesting = 0;
        };

        class Class {
            int callNesting = 0;
        };

        enum Enum {
            something
        };

        union Union {
            int a;
        };

        enum class EnumClass {
            something
        };

        SomeClass::SomeClass() {
            something;
        }

        SomeClass::~SomeClass() {
            something;
        }

        void f() {
            int a;
            if (a) {
                int b;
            } else {
                int c;
            }
            for (;;) {
                int d;
            }
            while (true) {
                int e;
            }
            do {
                int d;
            } while (true);
            switch (0) {
                case 1: break;
            }
        }
    )");

    auto test = [](const Node *node) {
        return (node->stype == +SrcmlCxxSType::Block);
    };
    CHECK(findNode(tree, test) == nullptr);
}

TEST_CASE("Constructors and destructors are moved to a separate layer",
          "[.srcml][srcml-cxx][parser]")
{
    Tree ctor = parseCxx(R"(
        SrcmlCxxLanguage::SrcmlCxxLanguage() {
            return;
        }
    )");
    Tree dtor = parseCxx(R"(
        SrcmlCxxLanguage::~SrcmlCxxLanguage() {
            return;
        }
    )");

    auto test = [](const Node *node) {
        return (node->stype == +SrcmlCxxSType::Constructor ||
                node->stype == +SrcmlCxxSType::Destructor)
            && node->children.empty()
            && node->next != nullptr;
    };
    CHECK(findNode(ctor, test) != nullptr);
    CHECK(findNode(dtor, test) != nullptr);
}

TEST_CASE("Braces of empty block are decomposed and stripped",
          "[.srcml][srcml-cxx][parser]")
{
    Tree tree = parseCxx(R"(
        void f() {
        }
    )");
    std::string expected = "\nvoid f() {\n}";

    TermHighlighter hi(*tree.getRoot(), *tree.getLanguage());
    REQUIRE(hi.print() == expected);
}

TEST_CASE("Work around incorrect parsing of then/else block",
          "[.srcml][srcml-cxx][parser]")
{
    std::string input = normalizeText(R"(
        class Column {
            inline friend void f() {
                if( first )
                    first = false;
                else
                    os << "\n";
            }
        };
    )");
    Tree tree = parseCxx(input);

    TermHighlighter hi(*tree.getRoot(), *tree.getLanguage());
    REQUIRE(hi.print() + '\n' == input);
}

TEST_CASE("Unicode characters are handled correctly",
          "[.srcml][srcml-cxx][parser]")
{
    std::string input = R"(auto a = "µs";)";
    Tree tree = parseCxx(input);

    TermHighlighter hi(*tree.getRoot(), *tree.getLanguage());
    REQUIRE(hi.print() == input);
}

TEST_CASE("Working around srcml bug of handling stdin input",
          "[.srcml][srcml-cxx][srcml-bug][parser]")
{
    {
        TempFile tmpFile("zograscope-test");
        std::ofstream ofs(tmpFile);
        ofs << "C::C(){}";
        ofs.close();

        cpp17::pmr::monolithic mr;
        std::unique_ptr<Language> lang = Language::create("test-file.cpp");
        CHECK_FALSE(lang->parse("C::C(){}\n", tmpFile, /*tabWidth=*/4, false,
                                mr).hasFailed());
    }

    {
        TempFile tmpFile("zograscope-test");
        std::ofstream ofs(tmpFile);
        ofs << "void\na::b()\n{\n}";
        ofs.close();

        cpp17::pmr::monolithic mr;
        std::unique_ptr<Language> lang = Language::create("test-file.cpp");
        CHECK_FALSE(lang->parse("void\na::b()\n{\n}", tmpFile, /*tabWidth=*/4,
                                false, mr).hasFailed());
    }
}

TEST_CASE("EOL continuation is identified in C++",
          "[.srcml][srcml-cxx][parser]")
{
    Tree tree = parseCxx(R"(
        int a \
          ;
    )");
    const Node *node = findNode(tree, [](const Node *node) {
                                    return node->label == "\\";
                                });
    REQUIRE(node != nullptr);
    CHECK(tree.getLanguage()->isEolContinuation(node));
}

TEST_CASE("Enum classes are properly handled", "[.srcml][srcml-cxx][parser]")
{
    Tree tree = parseCxx(R"(
        enum     class C : int {
            item,
        };
    )");
    CHECK(findNode(tree, makePred(Type::Keywords, "enum")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Keywords, "class")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Identifiers, "C")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Other, ":")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Keywords, "int")) != nullptr);
    CHECK(findNode(tree, makePred(Type::LeftBrackets, "{")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Identifiers, "item")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Other, ",")) != nullptr);
    CHECK(findNode(tree, makePred(Type::RightBrackets, "}")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Other, ";")) != nullptr);

    tree = parseCxx("enum class SType : std::uint8_t;");
    CHECK(findNode(tree, makePred(Type::Keywords, "enum")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Keywords, "class")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Identifiers, "SType")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Other, ":")) != nullptr);
    CHECK(findNode(tree, makePred(Type::UserTypes, "std")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Virtual, "::")) != nullptr);
    CHECK(findNode(tree, makePred(Type::UserTypes, "uint8_t")) != nullptr);
    CHECK(findNode(tree, makePred(Type::Other, ";")) != nullptr);
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/zograscope

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/zograscope

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master