xaizek / zograscope (License: AGPLv3 only) (since 2018-12-07)
Mainly a syntax-aware diff that also provides a number of additional tools.
<root> / tests / make / make-parser.cpp (e2614a88cd3aa20725f6620b4e197fe69ecc075b) (22KiB) (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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
// 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 <boost/algorithm/string/predicate.hpp>

#include <iostream>

#include "make/MakeSType.hpp"
#include "utils/strings.hpp"
#include "STree.hpp"
#include "TermHighlighter.hpp"
#include "tree.hpp"

#include "tests.hpp"

using namespace makestypes;

TEST_CASE("Error is printed on incorrect Makefile syntax", "[make][parser]")
{
    StreamCapture cerrCapture(std::cerr);
    CHECK_FALSE(makeIsParsed(":"));
    REQUIRE(boost::starts_with(cerrCapture.get(), "<input>:1:1:"));
}

TEST_CASE("Empty Makefile is OK", "[make][parser]")
{
    CHECK(makeIsParsed(""));
    CHECK(makeIsParsed("      "));
    CHECK(makeIsParsed("   \n   "));
    CHECK(makeIsParsed("\t\n \t \n"));
}

TEST_CASE("Root always has Makefile stype", "[make][parser]")
{
    Tree tree;

    tree = parseMake("");
    CHECK(tree.getRoot()->stype == +MakeSType::Makefile);

    tree = parseMake("a = b");
    CHECK(tree.getRoot()->stype == +MakeSType::Makefile);
}

TEST_CASE("Useless empty temporary containers are dropped", "[make][parser]")
{
    auto pred = [](const Node *node) {
        return node->stype == +MakeSType::TemporaryContainer
            && node->label.empty();
    };

    Tree tree = parseMake("set := to this");
    CHECK(findNode(tree, pred) == nullptr);
}

TEST_CASE("Comments are parsed in a Makefile", "[make][parser]")
{
    CHECK(makeIsParsed(" # comment"));
    CHECK(makeIsParsed("# comment "));
    CHECK(makeIsParsed(" # comment "));

    const char *const multiline = R"(
        a := a#b \
               asdf
    )";
    CHECK(makeIsParsed(multiline));
}

TEST_CASE("Assignments are parsed in a Makefile", "[make][parser]")
{
    SECTION("Whitespace around assignment statement") {
        CHECK(makeIsParsed("OS := os "));
        CHECK(makeIsParsed(" OS := os "));
        CHECK(makeIsParsed(" OS := os"));
    }
    SECTION("Whitespace around assignment") {
        CHECK(makeIsParsed("OS := os"));
        CHECK(makeIsParsed("OS:= os"));
        CHECK(makeIsParsed("OS :=os"));
    }
    SECTION("Various assignment kinds") {
        CHECK(makeIsParsed("CXXFLAGS = $(CFLAGS)"));
        CHECK(makeIsParsed("CXXFLAGS += -std=c++11 a:b"));
        CHECK(makeIsParsed("CXX ?= g++"));
        CHECK(makeIsParsed("CXX ::= g++"));
        CHECK(makeIsParsed("CXX != whereis g++"));
    }
    SECTION("One variable") {
        CHECK(makeIsParsed("CXXFLAGS = prefix$(CFLAGS)"));
        CHECK(makeIsParsed("CXXFLAGS = prefix$(CFLAGS)suffix"));
        CHECK(makeIsParsed("CXXFLAGS = $(CFLAGS)suffix"));
    }
    SECTION("Two variables") {
        CHECK(makeIsParsed("CXXFLAGS = $(var1)$(var2)"));
        CHECK(makeIsParsed("CXXFLAGS = $(var1)val$(var2)"));
        CHECK(makeIsParsed("CXXFLAGS = val$(var1)$(var2)val"));
        CHECK(makeIsParsed("CXXFLAGS = val$(var1)val$(var2)val"));
    }
    SECTION("Override and/or export") {
        CHECK(makeIsParsed("override CXXFLAGS ::="));
        CHECK(makeIsParsed("override CXXFLAGS = $(CFLAGS)suffix"));
        CHECK(makeIsParsed("override export CXXFLAGS = $(CFLAGS)suffix"));
        CHECK(makeIsParsed("export override CXXFLAGS = $(CFLAGS)suffix"));
        CHECK(makeIsParsed("export CXXFLAGS = $(CFLAGS)suffix"));
    }
    SECTION("Keywords in the value") {
        CHECK(makeIsParsed("KEYWORDS := ifdef/ifndef/ifeq/ifneq/else/endif"));
        CHECK(makeIsParsed("KEYWORDS += include"));
        CHECK(makeIsParsed("KEYWORDS += override/export/unexport"));
        CHECK(makeIsParsed("KEYWORDS += define/endef/undefine"));
    }
    SECTION("Keywords in the name") {
        CHECK(makeIsParsed("a.ifndef.b = a"));
        CHECK(makeIsParsed("ifndef.b = a"));
        CHECK(makeIsParsed("ifndef/b = a"));
        CHECK(makeIsParsed(",ifdef,ifndef,ifeq,ifneq,else,endif = b"));
        CHECK(makeIsParsed(",override,include,define,endef,undefine = b"));
        CHECK(makeIsParsed(",export,unexport = b"));
    }
    SECTION("Functions in the name") {
        CHECK(makeIsParsed(R"($(1).stuff :=)"));
    }
    SECTION("Special symbols in the value") {
        CHECK(makeIsParsed(R"(var += sed_first='s,^\([^/]*\)/.*$$,\1,';)"));
        CHECK(makeIsParsed(R"(var != test $$# -gt 0)"));
    }
    SECTION("Whitespace around assignment statement") {
        Tree tree;

        tree = parseMake("var := #comment");
        CHECK(findNode(tree, Type::Comments, "#comment") != nullptr);

        tree = parseMake("var := val#comment");
        CHECK(findNode(tree, Type::Comments, "#comment") != nullptr);

        tree = parseMake("override var := #comment");
        CHECK(findNode(tree, Type::Comments, "#comment") != nullptr);

        tree = parseMake("export var := val #comment");
        CHECK(findNode(tree, Type::Comments, "#comment") != nullptr);
    }
}

TEST_CASE("Variables are parsed in a Makefile", "[make][parser]")
{
    CHECK(makeIsParsed("$($(V))"));
    CHECK(makeIsParsed("$($(V)suffix)"));
    CHECK(makeIsParsed("$($(V)?endif)"));
    CHECK(makeIsParsed("$(bla?endif)"));
    CHECK(makeIsParsed("$(AT_$(V))"));
    CHECK(makeIsParsed("target: )()("));

    Tree tree = parseMake("target: $$($1.name)");
    CHECK(findNode(tree, Type::UserTypes, "$1") != nullptr);
    // This isn't a variable, it's an escape sequence.
    CHECK(findNode(tree, Type::UserTypes, "$$") == nullptr);
}

TEST_CASE("Functions are parsed in a Makefile", "[make][parser]")
{
    SECTION("Whitespace around function statement") {
        CHECK(makeIsParsed("$(info a)"));
        CHECK(makeIsParsed(" $(info a)"));
        CHECK(makeIsParsed(" $(info a) "));
        CHECK(makeIsParsed("$(info a) "));
        CHECK(makeIsParsed("$(info a )"));
    }
    SECTION("Whitespace around comma") {
        CHECK(makeIsParsed("$(info arg1,arg2)"));
        CHECK(makeIsParsed("$(info arg1, arg2)"));
        CHECK(makeIsParsed("$(info arg1 ,arg2)"));
        CHECK(makeIsParsed("$(info arg1 , arg2)"));
    }
    SECTION("Functions in arguments") {
        CHECK(makeIsParsed("$(info $(f1)$(f2))"));
    }
    SECTION("Empty arguments") {
        CHECK(makeIsParsed("$(patsubst , ,)"));
        CHECK(makeIsParsed("$(patsubst ,,)"));
        CHECK(makeIsParsed("$(patsubst a,,)"));
        CHECK(makeIsParsed("$(patsubst a,b,)"));
    }
    SECTION("Keywords in the arguments") {
        CHECK(makeIsParsed("$(info ifdef/ifndef/ifeq/ifneq/else/endif)"));
        CHECK(makeIsParsed("$(info include)"));
        CHECK(makeIsParsed("$(info override/export/unexport)"));
        CHECK(makeIsParsed("$(info define/endef/undefine)"));
    }
    SECTION("Curly braces") {
        Tree tree = parseMake("target: ${name}");
        CHECK(findNode(tree, Type::LeftBrackets, "${") != nullptr);
        CHECK(findNode(tree, Type::RightBrackets, "}") != nullptr);
    }
    SECTION("Nested parenthesis") {
        CHECK(makeIsParsed("$(info (b), ( (a) ) (c))"));
        CHECK(makeIsParsed("$(info (b), ( (a) ) (c), ( $(substr $(a),$(b),$(c)) ) )"));
        CHECK(makeIsParsed("$(foreach src, $(stmmac-srcs), ifeq ($(shell test $(R) -gt $(REV); echo $$?),0) )"));
    }
    SECTION("String literals in the arguments") {
        CHECK(makeIsParsed(R"($(patsubst "%",%,$(VAR)))"));
        CHECK(makeIsParsed(R"_(${shell "$(CC)"})_"));
        CHECK(makeIsParsed(R"($(error "str"))"));
    }
    SECTION("Parenthesis can be escaped in a call") {
        CHECK(makeIsParsed(R"_($(shell \( echo hi    ))_"));
        CHECK(makeIsParsed(R"_($(shell    echo hi \) ))_"));
        CHECK(makeIsParsed(R"_($(shell \( echo hi \) ))_"));
    }
}

TEST_CASE("Targets are parsed in a Makefile", "[make][parser]")
{
    SECTION("Whitespace around colon") {
        CHECK(makeIsParsed("target: prereq"));
        CHECK(makeIsParsed("target : prereq"));
        CHECK(makeIsParsed("target :prereq"));
    }
    SECTION("No prerequisites") {
        CHECK(makeIsParsed("target:"));
    }
    SECTION("Multiple prerequisites") {
        CHECK(makeIsParsed("target: prereq1 prereq2"));
    }
    SECTION("Multiple targets") {
        CHECK(makeIsParsed("debug release sanitize-basic: all"));
    }
    SECTION("Double-colon") {
        CHECK(makeIsParsed("debug:: all"));
        CHECK(makeIsParsed("debug :: all"));
        CHECK(makeIsParsed("debug ::all"));
    }
    SECTION("Keywords in targets") {
        CHECK(makeIsParsed("include.b: all"));
        CHECK(makeIsParsed("x.include.b: all"));
        CHECK(makeIsParsed("all: b.override.b"));
        CHECK(makeIsParsed("all: override.b"));
    }
    SECTION("Expressions in prerequisites and targets") {
        CHECK(makeIsParsed("target: $(dependencies)"));
        CHECK(makeIsParsed("$(target): dependencies"));
        CHECK(makeIsParsed("$(target): $(dependencies)"));
        CHECK(makeIsParsed("$(tar)$(get): $(dependencies)"));
    }
}

TEST_CASE("Static pattern rules are parsed", "[make][parser]")
{
    CHECK(makeIsParsed("target: target-pattern: prereq"));
    CHECK(makeIsParsed("$(AOBJS) $(UAOBJS) $(HEAD_OBJ): %$(OBJEXT): %.S"));
    CHECK(makeIsParsed("$(COBJS) $(UCOBJS): %$(OBJEXT): CFLAGS+=-O0"));
}

TEST_CASE("Recipes are parsed in a Makefile", "[make][parser]")
{
    const char *const singleLine = R"(
target: prereq
	the only recipe
    )";
    CHECK(makeIsParsed(singleLine));

    const char *const multipleLines = R"(
target: prereq
	first recipe
	second recipe
    )";
    CHECK(makeIsParsed(multipleLines));

    const char *const withComments = R"(
target: prereq
# comment
	first recipe
# comment
	second recipe
	# comment
    )";
    CHECK(makeIsParsed(withComments));

    const char *const withParens = R"(
target: prereq
	(echo something)
    )";
    CHECK(makeIsParsed(withParens));

    const char *const withFunctions = R"(
target: prereq
	$a$b
    )";
    CHECK(makeIsParsed(withFunctions));

    const char *const assignment = R"(
        $(out_dir)/tests/tests: EXTRA_CXXFLAGS += -Wno-error=parentheses
    )";
    CHECK(makeIsParsed(assignment));

    const char *const withStaticPattern = R"(
targets: target: prereq
	$a$b
    )";
    CHECK(makeIsParsed(withStaticPattern ));
}

TEST_CASE("Recipes with breaks", "[make][parser]")
{
    const char *const withTab = R"(
rule:
	@echo hi
	
	@echo there
    )";
    CHECK(makeIsParsed(withTab ));

    const char *const commands = R"(
rule:
	@echo hi

	@echo there
    )";
    CHECK(makeIsParsed(commands));

    const char *const comments = R"(
rule:
	#asdf

	#asdf
    )";
    CHECK(makeIsParsed(comments));

    const char *const conditonalsInRecipe = R"(
rule:
ifeq 'a' 'a'
	adf agdf
endif

ifeq 'a' 'a'
    a=10
endif
    )";
    CHECK(makeIsParsed(conditonalsInRecipe));

    const char *const conditionalsInAndAfterRecipe = R"(
rule:
ifeq 'a' 'a'
	adf agdf
endif

ifeq 'a' 'a'
    a=10
endif
    )";
    CHECK(makeIsParsed(conditionalsInAndAfterRecipe ));
}

TEST_CASE("Conditionals are parsed in a Makefile", "[make][parser]")
{
    const char *const withBody_withoutElse = R"(
        ifneq ($(OS),Windows_NT)
            bin_suffix :=
        endif
    )";
    CHECK(makeIsParsed(withBody_withoutElse));

    const char *const withoutBody_withoutElse = R"(
        ifneq ($(OS),Windows_NT)
        endif
    )";
    CHECK(makeIsParsed(withoutBody_withoutElse));

    const char *const withBody_withElse = R"(
        ifndef tool_template
            bin_suffix :=
        else
            bin_suffix := .exe
        endif
    )";
    CHECK(makeIsParsed(withBody_withElse));

    const char *const withoutBody_withElse = R"(
        ifeq ($(OS),Windows_NT)
            bin_suffix :=
        else
        endif
    )";
    CHECK(makeIsParsed(withoutBody_withElse));

    const char *const nested = R"(
        ifneq ($(OS),Windows_NT)
            ifdef OS
            endif
        endif
    )";
    CHECK(makeIsParsed(nested));

    const char *const emptyArgs = R"(
        ifneq ($(OS),)
        endif
        ifneq (,$(OS))
        endif
        ifneq (,)
        endif
    )";
    CHECK(makeIsParsed(emptyArgs));

    const char *const elseIf = R"(
        ifneq ($(OS),)
        else ifndef OS
        else ifeq (,)
        endif
    )";
    CHECK(makeIsParsed(elseIf));

    const char *const withComments = R"(
        ifneq ($(OS),)  # ifneq-comment
        else            # else-comment
        endif           # endif-comment
    )";
    CHECK(makeIsParsed(withComments));

    const char *const conditionalInRecipes = R"(
reset-coverage:
ifeq ($(with_cov),1)
	find $(out_dir)/ -name '*.gcda' -delete
endif
    )";
    CHECK(makeIsParsed(conditionalInRecipes));

    const char *const conditionalsInRecipes = R"(
reset-coverage:
ifdef tool_template
	find $(out_dir)/ -name '*.gcda' -delete
endif
ifeq ($(with_cov),1)
	find $(out_dir)/ -name '*.gcda' -delete
else
endif
    )";
    CHECK(makeIsParsed(conditionalsInRecipes));

    const char *const elseIfInRecipes = R"(
reset-coverage:
ifdef tool_template
	find $(out_dir)/ -name '*.gcda' -delete
else ifeq ($(with_cov),1)
	find $(out_dir)/ -name '*.gcda' -delete
else ifdef something
else
endif
    )";
    CHECK(makeIsParsed(elseIfInRecipes));

    const char *const conditionalInRecipesWithComments = R"(
reset-coverage:
ifeq ($(with_cov),1)  # ifeq-comment
	find $(out_dir)/ -name '*.gcda' -delete
else                  # else-comment
endif                 # endif-comment
    )";
    CHECK(makeIsParsed(conditionalInRecipesWithComments));

    const char *const alternativeForm = R"(
        ifneq 'a' 'b'
        endif
        ifeq 'a' "b"
        endif
        ifneq "a" 'b'
        endif
        ifneq "a" "b"
        endif

        ifneq 'a''b'
        endif
        ifeq 'a'"b"
        endif
        ifneq "a"'b'
        endif
        ifneq "a""b"
        endif
    )";
    CHECK(makeIsParsed(alternativeForm));
}

TEST_CASE("Defines are parsed in a Makefile", "[make][parser]")
{
    const char *const noBody = R"(
        define pattern
        endef
    )";
    CHECK(makeIsParsed(noBody));

    const char *const noSuffix = R"(
        define pattern
            bin_suffix :=
        endef
    )";
    CHECK(makeIsParsed(noSuffix));

    const char *const weirdName = R"(
        define )name
        endef
    )";
    CHECK(makeIsParsed(weirdName));

    const char *const withOverride = R"(
        override define pattern ?=
        endef
    )";
    CHECK(makeIsParsed(withOverride));

    const char *const withExport = R"(
        export define pattern
        endef
    )";
    CHECK(makeIsParsed(withExport));

    const char *const withOverrideAndExport = R"(
        export override define pattern
        endef
    )";
    CHECK(makeIsParsed(withOverrideAndExport));

    const char *const eqSuffix = R"(
        define pattern =
            bin_suffix :=
        endef
    )";
    CHECK(makeIsParsed(eqSuffix));

    const char *const immSuffix = R"(
        define pattern :=
        endef

        define pattern ::=
            bin_suffix :=
        endef
    )";
    CHECK(makeIsParsed(immSuffix));

    const char *const appendSuffix = R"(
        define pattern +=
            bin_suffix :=
        endef
    )";
    CHECK(makeIsParsed(appendSuffix));

    const char *const bangSuffix = R"(
        define pattern +=
        endef
    )";
    Tree tree = parseMake(bangSuffix);
    CHECK(findNode(tree, Type::Assignments, "+=") != nullptr);

    const char *const expresion = R"(
        define tool_template
            $1.bin
        endef
    )";
    CHECK(makeIsParsed(expresion));

    const char *const expresions = R"(
        define vifm_SOURCES :=
            $(cfg) $(compat) $(engine) endef $(int) $(io) $(menus) $(modes)
        endef
    )";
    CHECK(makeIsParsed(expresions));

    const char *const textFirstEmptyLine = R"(
        define vifm_SOURCES :=

            bla $(cfg) $(compat) $(engine) endef $(int) $(io) $(menus) $(modes)
        endef
    )";
    CHECK(makeIsParsed(textFirstEmptyLine));

    const char *const multipleEmptyLines = R"(
        define vifm_SOURCES :=


            bla$(cfg) $(compat) $(engine) endef $(int) $(io) $(menus) $(modes)
        endef
    )";
    CHECK(makeIsParsed(multipleEmptyLines));

    const char *const emptyLinesEverywhere = R"(
        define vifm_SOURCES :=

            $(cfg) $(compat) $(engine) endef $(int) $(io) $(menus) $(modes)

            something

        endef
    )";
    CHECK(makeIsParsed(emptyLinesEverywhere));

    const char *const keywordsInName = R"(
        define keywords
            (
            )
            ,
            # comment
            include
            override
            export
            unexport
            ifdef
            ifndef
            ifeq
            ifneq
            else
            endif
            define
            undefine
        endef
    )";
    CHECK(makeIsParsed(keywordsInName));
}

TEST_CASE("Line escaping works in a Makefile", "[make][parser]")
{
    const char *const multiline = R"(
        pos = $(strip $(eval T := ) \
                      $(eval i := -1) \
                      $(foreach elem, $1, \
                                $(if $(filter $2,$(elem)), \
                                              $(eval i := $(words $T)), \
                                              $(eval T := $T $(elem)))) \
                      $i)
    )";
    CHECK(makeIsParsed(multiline));
}

TEST_CASE("Substitutions are parsed in a Makefile", "[make][parser]")
{
    CHECK(makeIsParsed("lib_objects := $(lib_sources:%.cpp=$(out_dir)/%.o)"));
    CHECK(makeIsParsed("am__test_logs1 = $(TESTS:=.log)"));
}

TEST_CASE("Arguments are not treated as substitutions", "[make][parser]")
{
    CHECK(makeIsParsed("$(a :)"));
    CHECK(makeIsParsed("$(a ::)"));
    CHECK(makeIsParsed("$(a ::,:)"));
    CHECK(makeIsParsed("$(a ::, :)"));
    CHECK(makeIsParsed("$(subst :,,$(VPATH))"));
    CHECK(makeIsParsed("$(subst :,:,$(VPATH))"));
}

TEST_CASE("Makefile keywords are not found inside text/ids", "[make][parser]")
{
    CHECK(makeIsParsed("EXTRA_CXXFLAGS += -fsanitize=undefined"));
}

TEST_CASE("Includes are parsed in a Makefile", "[make][parser]")
{
    CHECK(makeIsParsed("include $(wildcard *.d)"));
    CHECK(makeIsParsed("include config.mk"));
    CHECK(makeIsParsed("-include config.mk"));
    CHECK(makeIsParsed("include include"));
}

TEST_CASE("Leading tabs are allowed not only for recipes in Makefiles",
          "[make][parser]")
{
    const char *const conditional = R"(
	ifeq (,$(findstring gcc,$(CC)))
		set := to this
	endif
    )";
    CHECK(makeIsParsed(conditional));

    const char *const emptyConditional = R"(
	ifeq (,$(findstring gcc,$(CC)))
	endif
    )";
    CHECK(makeIsParsed(emptyConditional));

    const char *const statements = R"(
	set := to this
	# comment
    )";
    CHECK(makeIsParsed(statements));
}

TEST_CASE("Column of recipe is computed correctly", "[make][parser]")
{
    std::string input = R"(
target:
	command1
	command2
    )";
    std::string expected = R"(
target:
    command1
    command2)";

    Tree tree = parseMake(input);

    std::string output = TermHighlighter(tree).print();
    CHECK(split(output, '\n') == split(expected, '\n'));
}

TEST_CASE("Export/unexport directives", "[make][parser]")
{
    CHECK(makeIsParsed("export"));
    CHECK(makeIsParsed("export var"));
    CHECK(makeIsParsed("export $(vars)"));
    CHECK(makeIsParsed("export var $(vars)"));
    CHECK(makeIsParsed("export var$(vars)"));

    CHECK(makeIsParsed("unexport"));
    CHECK(makeIsParsed("unexport var"));
    CHECK(makeIsParsed("unexport $(vars)"));
    CHECK(makeIsParsed("unexport var $(vars)"));
    CHECK(makeIsParsed("unexport var$(vars)"));
}

TEST_CASE("Undefine directive", "[make][parser]")
{
    CHECK(makeIsParsed("undefine something"));
    CHECK(makeIsParsed("undefine this and that"));
    CHECK(makeIsParsed("undefine some $(things)"));
    CHECK(makeIsParsed("undefine override something"));
    CHECK(makeIsParsed("override undefine something"));
}

TEST_CASE("Strings are recognized by the parser", "[make][parser]")
{
    Tree tree;

    tree = parseMake(R"(
target:
	echo \'define VERSION "0.9" > $@;
    )");
    CHECK(findNode(tree, Type::StrConstants, "\"0.9\"") != nullptr);

    tree = parseMake(R"(
target:
	echo \'define VERSION "0.9.1-beta"' > $@;
    )");
    CHECK(findNode(tree, Type::StrConstants,
                   "'define VERSION \"0.9.1-beta\"'") != nullptr);
}

TEST_CASE("EOL continuation is identified in GNU Make", "[make][parser]")
{
    Tree tree = parseMake(R"(
        a = \
          b
    )");
    const Node *node = findNode(tree, [](const Node *node) {
                                    return node->label == "\\";
                                });
    REQUIRE(node != nullptr);
    CHECK(tree.getLanguage()->isEolContinuation(node));
}

TEST_CASE("New line node does not appear in the tree", "[make][parser]")
{
    Tree tree = parseMake(R"(
        define suite_template
           a

        endef
    )");
    const Node *node = findNode(tree, [](const Node *node) {
                                    return node->label == "\n";
                                });
    REQUIRE(node == nullptr);
}

TEST_CASE("Tabulation of size 1 is allowed in Makefiles", "[make][parser]")
{
    int tabWidth = 1;

    const char *const str = ""
        "# 0\n"
        "\t# 1\n"
        "\t\t# 2\n"
    ;

    cpp17::pmr::monolithic mr;
    std::unique_ptr<Language> lang = Language::create("Makefile");

    TreeBuilder tb = lang->parse(str, "<input>", tabWidth, /*debug=*/false, mr);
    REQUIRE_FALSE(tb.hasFailed());

    STree stree(std::move(tb), str, false, false, *lang, mr);
    Tree tree(std::move(lang), tabWidth, str, stree.getRoot());

    const Node *node;

    node = findNode(tree, Type::Comments, "# 0");
    REQUIRE(node);
    CHECK(node->col == 1);

    node = findNode(tree, Type::Comments, "# 1");
    REQUIRE(node);
    CHECK(node->col == 2);

    node = findNode(tree, Type::Comments, "# 2");
    REQUIRE(node);
    CHECK(node->col == 3);
}
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