Hash :
b980c563
        
        Author :
  
        
        Date :
2018-11-27T11:34:27
        
      
Reformat all cpp and h files. This applies git cl format --full to all ANGLE sources. Bug: angleproject:2986 Change-Id: Ib504e618c1589332a37e97696cdc3515d739308f Reviewed-on: https://chromium-review.googlesource.com/c/1351367 Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
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
//
// Copyright (c) 2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// RemoveUnreferencedVariables_test.cpp:
//   Tests for removing unreferenced variables from the AST.
//
#include "GLSLANG/ShaderLang.h"
#include "angle_gl.h"
#include "gtest/gtest.h"
#include "tests/test_utils/compiler_test.h"
using namespace sh;
class RemoveUnreferencedVariablesTest : public MatchOutputCodeTest
{
  public:
    RemoveUnreferencedVariablesTest() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, 0, SH_ESSL_OUTPUT)
    {}
};
// Test that a simple unreferenced declaration is pruned.
TEST_F(RemoveUnreferencedVariablesTest, SimpleDeclaration)
{
    const std::string &shaderString =
        R"(precision mediump float;
        void main()
        {
            vec4 myUnreferencedVec;
        })";
    compile(shaderString);
    ASSERT_TRUE(notFoundInCode("myUnreferencedVec"));
}
// Test that a simple unreferenced global declaration is pruned.
TEST_F(RemoveUnreferencedVariablesTest, SimpleGlobalDeclaration)
{
    const std::string &shaderString =
        R"(precision mediump float;
        vec4 myUnreferencedVec;
        void main()
        {
        })";
    compile(shaderString);
    ASSERT_TRUE(notFoundInCode("myUnreferencedVec"));
}
// Test that a simple unreferenced variable with an initializer is pruned.
TEST_F(RemoveUnreferencedVariablesTest, SimpleInitializer)
{
    const std::string &shaderString =
        R"(precision mediump float;
        uniform vec4 uVec;
        void main()
        {
            vec4 myUnreferencedVec = uVec;
        })";
    compile(shaderString);
    ASSERT_TRUE(notFoundInCode("myUnreferencedVec"));
}
// Test that a user-defined function call inside an unreferenced variable initializer is retained.
TEST_F(RemoveUnreferencedVariablesTest, SideEffectInInitializer)
{
    const std::string &shaderString =
        R"(precision mediump float;
        vec4 sideEffect(int i)
        {
            gl_FragColor = vec4(0, i, 0, 1);
            return vec4(0);
        }
        void main()
        {
            vec4 myUnreferencedVec = sideEffect(1);
        })";
    compile(shaderString);
    // We're happy as long as the function with side effects is called.
    ASSERT_TRUE(foundInCode("sideEffect(1)"));
}
// Test that a modf call inside an unreferenced variable initializer is retained.
TEST_F(RemoveUnreferencedVariablesTest, BuiltInSideEffectInInitializer)
{
    const std::string &shaderString =
        R"(#version 300 es
        precision mediump float;
        uniform float uF;
        out vec4 my_FragColor;
        void main()
        {
            float iPart = 0.0;
            float myUnreferencedFloat = modf(uF, iPart);
            my_FragColor = vec4(0.0, iPart, 0.0, 1.0);
        })";
    compile(shaderString);
    // We're happy as long as the function with side effects is called.
    ASSERT_TRUE(foundInCode("modf("));
}
// Test that an imageStore call inside an unreferenced variable initializer is retained.
TEST_F(RemoveUnreferencedVariablesTest, ImageStoreSideEffectInInitializer)
{
    const std::string &shaderString =
        R"(#version 310 es
        precision highp float;
        layout(rgba32i) uniform highp writeonly iimage2D img;
        void main()
        {
            float myUnreferencedFloat = (imageStore(img, ivec2(0), ivec4(1)), 1.0);
        })";
    compile(shaderString);
    // We're happy as long as the function with side effects is called.
    ASSERT_TRUE(foundInCode("imageStore("));
}
// Test that multiple variables that are chained but otherwise are unreferenced are removed.
TEST_F(RemoveUnreferencedVariablesTest, MultipleVariablesChained)
{
    const std::string &shaderString =
        R"(precision mediump float;
        uniform vec4 uVec;
        void main()
        {
            vec4 myUnreferencedVec1 = uVec;
            vec4 myUnreferencedVec2 = myUnreferencedVec1 * 2.0;
            vec4 myUnreferencedVec3 = myUnreferencedVec2 + 1.0;
        })";
    compile(shaderString);
    ASSERT_TRUE(notFoundInCode("myUnreferencedVec3"));
    ASSERT_TRUE(notFoundInCode("myUnreferencedVec2"));
    ASSERT_TRUE(notFoundInCode("myUnreferencedVec1"));
}
// Test that multiple variables that are chained with the last one being referenced are kept.
TEST_F(RemoveUnreferencedVariablesTest, MultipleVariablesChainedReferenced)
{
    const std::string &shaderString =
        R"(precision mediump float;
        uniform vec4 uVec;
        void main()
        {
            vec4 myReferencedVec1 = uVec;
            vec4 myReferencedVec2 = myReferencedVec1 * 2.0;
            vec4 myReferencedVec3 = myReferencedVec2 + 1.0;
            gl_FragColor = myReferencedVec3;
        })";
    compile(shaderString);
    ASSERT_TRUE(foundInCode("myReferencedVec3"));
    ASSERT_TRUE(foundInCode("myReferencedVec2"));
    ASSERT_TRUE(foundInCode("myReferencedVec1"));
}
// Test that multiple variables that are chained within two scopes but otherwise are unreferenced
// are removed.
TEST_F(RemoveUnreferencedVariablesTest, MultipleVariablesChainedTwoScopes)
{
    const std::string &shaderString =
        R"(precision mediump float;
        uniform vec4 uVec;
        void main()
        {
            vec4 myUnreferencedVec1 = uVec;
            vec4 myUnreferencedVec2 = myUnreferencedVec1 * 2.0;
            if (uVec.x > 0.0)
            {
                vec4 myUnreferencedVec3 = myUnreferencedVec2 + 1.0;
            }
        })";
    compile(shaderString);
    ASSERT_TRUE(notFoundInCode("myUnreferencedVec3"));
    ASSERT_TRUE(notFoundInCode("myUnreferencedVec2"));
    ASSERT_TRUE(notFoundInCode("myUnreferencedVec1"));
}
// Test that multiple variables that are chained with the last one being referenced in an inner
// scope are kept.
TEST_F(RemoveUnreferencedVariablesTest, VariableReferencedInAnotherScope)
{
    const std::string &shaderString =
        R"(precision mediump float;
        uniform vec4 uVec;
        void main()
        {
            vec4 myReferencedVec1 = uVec;
            vec4 myReferencedVec2 = myReferencedVec1 * 2.0;
            if (uVec.x > 0.0)
            {
                vec4 myReferencedVec3 = myReferencedVec2 + 1.0;
                gl_FragColor = myReferencedVec3;
            }
        })";
    compile(shaderString);
    ASSERT_TRUE(foundInCode("myReferencedVec3"));
    ASSERT_TRUE(foundInCode("myReferencedVec2"));
    ASSERT_TRUE(foundInCode("myReferencedVec1"));
}
// Test that if there are two variables with the same name, one of them can be removed and another
// one kept.
TEST_F(RemoveUnreferencedVariablesTest, TwoVariablesWithSameNameInDifferentScopes)
{
    const std::string &shaderString =
        R"(precision mediump float;
        uniform vec4 uVec;
        void main()
        {
            vec4 myVec = uVec;  // This one is unreferenced.
            if (uVec.x > 0.0)
            {
                vec4 myVec = uVec * 2.0;  // This one is referenced.
                gl_FragColor = myVec;
            }
            vec4 myUnreferencedVec = myVec;
        })";
    compile(shaderString);
    ASSERT_TRUE(foundInCode("myVec", 2));
}
// Test that an unreferenced variable declared in a for loop header is removed.
TEST_F(RemoveUnreferencedVariablesTest, UnreferencedVariableDeclaredInForLoopHeader)
{
    const std::string &shaderString =
        R"(#version 300 es
        precision highp float;
        uniform int ui;
        out vec4 my_FragColor;
        void main()
        {
            my_FragColor = vec4(0.0);
            int index = 0;
            for (int unreferencedInt = ui; index < 10; ++index)
            {
                my_FragColor += vec4(0.0, float(index) * 0.01, 0.0, 0.0);
            }
        })";
    compile(shaderString);
    ASSERT_TRUE(foundInCode("index"));
    ASSERT_TRUE(notFoundInCode("unreferencedInt"));
}
// Test that a loop condition is kept even if it declares an unreferenced variable.
TEST_F(RemoveUnreferencedVariablesTest, UnreferencedVariableDeclaredInWhileLoopCondition)
{
    const std::string &shaderString =
        R"(#version 300 es
        precision highp float;
        uniform int ui;
        out vec4 my_FragColor;
        void main()
        {
            my_FragColor = vec4(0.0);
            int index = 0;
            while (bool b = (index < 10))
            {
                my_FragColor += vec4(0.0, float(index) * 0.01, 0.0, 0.0);
                ++index;
            }
        })";
    compile(shaderString);
    ASSERT_TRUE(foundInCode("index < 10"));
}
// Test that a variable declared in a for loop header that is only referenced in an unreferenced
// variable initializer is removed.
TEST_F(RemoveUnreferencedVariablesTest,
       VariableDeclaredInForLoopHeaderAccessedInUnreferencedVariableInitializer)
{
    const std::string &shaderString =
        R"(#version 300 es
        precision highp float;
        uniform int ui;
        out vec4 my_FragColor;
        void main()
        {
            my_FragColor = vec4(0.0);
            int index = 0;
            for (int unreferencedInt1 = ui; index < 10; ++index)
            {
                int unreferencedInt2 = unreferencedInt1;
                my_FragColor += vec4(0.0, float(index) * 0.01, 0.0, 0.0);
            }
        })";
    compile(shaderString);
    ASSERT_TRUE(foundInCode("index"));
    ASSERT_TRUE(notFoundInCode("unreferencedInt2"));
    ASSERT_TRUE(notFoundInCode("unreferencedInt1"));
}
// Test that a user-defined type (struct) declaration that's used is not removed, but that the
// variable that's declared in the same declaration is removed.
TEST_F(RemoveUnreferencedVariablesTest, UserDefinedTypeReferencedAndVariableNotReferenced)
{
    const std::string &shaderString =
        R"(#version 300 es
        precision highp float;
        uniform float uF;
        out vec4 my_FragColor;
        void main()
        {
            struct myStruct { float member; } unreferencedStruct;
            myStruct usedStruct = myStruct(uF);
            my_FragColor = vec4(usedStruct.member);
        })";
    compile(shaderString);
    ASSERT_TRUE(foundInCode("myStruct"));
    ASSERT_TRUE(foundInCode("usedStruct"));
    ASSERT_TRUE(notFoundInCode("unreferencedStruct"));
}
// Test that a nameless user-defined type (struct) declaration is removed entirely.
TEST_F(RemoveUnreferencedVariablesTest, NamelessUserDefinedTypeUnreferenced)
{
    const std::string &shaderString =
        R"(#version 300 es
        precision highp float;
        void main()
        {
            struct { float member; } unreferencedStruct;
        })";
    compile(shaderString);
    ASSERT_TRUE(notFoundInCode("unreferencedStruct"));
    ASSERT_TRUE(notFoundInCode("member"));
}
// Test that a variable that's only referenced in a unused function is removed.
TEST_F(RemoveUnreferencedVariablesTest, VariableOnlyReferencedInUnusedFunction)
{
    const std::string &shaderString =
        R"(
        int onlyReferencedInUnusedFunction = 0;
        void unusedFunc() {
            onlyReferencedInUnusedFunction++;
        }
        void main()
        {
        })";
    compile(shaderString);
    ASSERT_TRUE(notFoundInCode("onlyReferencedInUnusedFunction"));
}
// Test that a variable that's only referenced in an array length() method call is removed.
TEST_F(RemoveUnreferencedVariablesTest, VariableOnlyReferencedInLengthMethod)
{
    const std::string &shaderString =
        R"(#version 300 es
        precision highp float;
        out vec4 my_FragColor;
        void main()
        {
            float onlyReferencedInLengthMethodCall[1];
            int len = onlyReferencedInLengthMethodCall.length();
            my_FragColor = vec4(0, len, 0, 1);
        })";
    compile(shaderString);
    ASSERT_TRUE(notFoundInCode("onlyReferencedInLengthMethodCall"));
}
// Test that an unreferenced user-defined type is removed.
TEST_F(RemoveUnreferencedVariablesTest, UserDefinedTypeUnreferenced)
{
    const std::string &shaderString =
        R"(
        struct myStructType
        {
            int i;
        } myStructVariable;
        void main()
        {
        })";
    compile(shaderString);
    ASSERT_TRUE(notFoundInCode("myStructType"));
}
// Test that a user-defined type that's only referenced in an unreferenced variable is removed.
TEST_F(RemoveUnreferencedVariablesTest, UserDefinedTypeReferencedInUnreferencedVariable)
{
    const std::string &shaderString =
        R"(
        struct myStructType
        {
            int i;
        };
        void main()
        {
            myStructType myStructVariable;
        })";
    compile(shaderString);
    ASSERT_TRUE(notFoundInCode("myStructType"));
}
// Test that a user-defined type that's declared in an empty declaration and that is only referenced
// in an unreferenced variable is removed also when the shader contains another independent
// user-defined type that's declared in an empty declaration. This tests special case handling of
// reference counting of empty symbols.
TEST_F(RemoveUnreferencedVariablesTest,
       TwoUserDefinedTypesDeclaredInEmptyDeclarationsWithOneOfThemUnreferenced)
{
    const std::string &shaderString =
        R"(
        struct myStructTypeA
        {
            int i;
        };
        struct myStructTypeB
        {
            int j;
        };
        uniform myStructTypeB myStructVariableB;
        void main()
        {
            myStructTypeA myStructVariableA;
        })";
    compile(shaderString);
    ASSERT_TRUE(notFoundInCode("myStructTypeA"));
    ASSERT_TRUE(foundInCode("myStructTypeB"));
}
// Test that a user-defined type that is only referenced in another unreferenced type is removed.
TEST_F(RemoveUnreferencedVariablesTest, UserDefinedTypeChain)
{
    const std::string &shaderString =
        R"(
        struct myInnerStructType
        {
            int i;
        };
        struct myOuterStructType
        {
            myInnerStructType inner;
        } myStructVariable;
        void main()
        {
            myOuterStructType myStructVariable2;
        })";
    compile(shaderString);
    ASSERT_TRUE(notFoundInCode("myInnerStructType"));
}
// Test that a user-defined type that is referenced in another user-defined type that is used is
// kept.
TEST_F(RemoveUnreferencedVariablesTest, UserDefinedTypeChainReferenced)
{
    const std::string &shaderString =
        R"(
        precision mediump float;
        struct myInnerStructType
        {
            int i;
        };
        uniform struct
        {
            myInnerStructType inner;
        } myStructVariable;
        void main()
        {
            if (myStructVariable.inner.i > 0)
            {
                gl_FragColor = vec4(0, 1, 0, 1);
            }
        })";
    compile(shaderString);
    ASSERT_TRUE(foundInCode("struct _umyInnerStructType"));
}
// Test that a struct type that is only referenced in a constructor and function call is kept.
TEST_F(RemoveUnreferencedVariablesTest, UserDefinedTypeReferencedInConstructorAndCall)
{
    const std::string &shaderString =
        R"(
        precision mediump float;
        uniform int ui;
        struct myStructType
        {
            int iMember;
        };
        void func(myStructType myStructParam)
        {
            if (myStructParam.iMember > 0)
            {
                gl_FragColor = vec4(0, 1, 0, 1);
            }
        }
        void main()
        {
            func(myStructType(ui));
        })";
    compile(shaderString);
    ASSERT_TRUE(foundInCode("struct _umyStructType"));
}
// Test that a struct type that is only referenced in a constructor is kept. This assumes that there
// isn't more sophisticated folding of struct field access going on.
TEST_F(RemoveUnreferencedVariablesTest, UserDefinedTypeReferencedInConstructor)
{
    const std::string &shaderString =
        R"(
        precision mediump float;
        uniform int ui;
        struct myStructType
        {
            int iMember;
        };
        void main()
        {
            if (myStructType(ui).iMember > 0)
            {
                gl_FragColor = vec4(0, 1, 0, 1);
            }
        })";
    compile(shaderString);
    ASSERT_TRUE(foundInCode("struct _umyStructType"));
}
// Test that a struct type that is only referenced in an unused function is removed.
TEST_F(RemoveUnreferencedVariablesTest, UserDefinedTypeReferencedInUnusedFunction)
{
    const std::string &shaderString =
        R"(
        precision mediump float;
        struct myStructType
        {
            int iMember;
        };
        void func(myStructType myStructParam)
        {
            if (myStructParam.iMember > 0)
            {
                gl_FragColor = vec4(0, 1, 0, 1);
            }
        }
        void main()
        {
        })";
    compile(shaderString);
    ASSERT_TRUE(notFoundInCode("myStructType"));
}
// Test that a struct type that is only referenced in an unused function is kept in case
// SH_DONT_PRUNE_UNUSED_FUNCTIONS is specified.
TEST_F(RemoveUnreferencedVariablesTest, UserDefinedTypeReferencedInUnusedFunctionThatIsNotPruned)
{
    const std::string &shaderString =
        R"(
        struct myStructType
        {
            int iMember;
        };
        myStructType func()
        {
            return myStructType(0);
        }
        void main()
        {
        })";
    compile(shaderString, SH_DONT_PRUNE_UNUSED_FUNCTIONS);
    ASSERT_TRUE(foundInCode("struct _umyStructType"));
    // Ensure that the struct isn't declared as a part of the function header.
    ASSERT_TRUE(foundInCode("};"));
}
// Test that a struct type that is only referenced as a function return value is kept.
TEST_F(RemoveUnreferencedVariablesTest, UserDefinedTypeReturnedFromFunction)
{
    const std::string &shaderString =
        R"(
        precision mediump float;
        struct myStructType
        {
            int iMember;
        };
        myStructType func()
        {
            gl_FragColor = vec4(0, 1, 0, 1);
            return myStructType(0);
        }
        void main()
        {
            func();
        })";
    compile(shaderString);
    ASSERT_TRUE(foundInCode("struct _umyStructType"));
    // Ensure that the struct isn't declared as a part of the function header.
    ASSERT_TRUE(foundInCode("};"));
}
// Test that a struct type that is only referenced in a uniform block is kept.
TEST_F(RemoveUnreferencedVariablesTest, UserDefinedTypeInUniformBlock)
{
    const std::string &shaderString =
        R"(#version 300 es
        precision highp float;
        out vec4 my_FragColor;
        struct myStructType
        {
            int iMember;
        };
        layout(std140) uniform myBlock {
            myStructType uStruct;
            int ui;
        };
        void main()
        {
            if (ui > 0)
            {
                my_FragColor = vec4(0, 1, 0, 1);
            }
        })";
    compile(shaderString);
    ASSERT_TRUE(foundInCode("struct _umyStructType"));
}
// Test that a struct type that is referenced from an initializer with a constructor can be removed.
TEST_F(RemoveUnreferencedVariablesTest, UserDefinedTypeConstructorInitializer)
{
    const std::string &shaderString =
        R"(#version 300 es
        precision highp float;
        out vec4 my_FragColor;
        struct myStructType
        {
            int iMember;
        };
        uniform int ui;
        void main()
        {
            myStructType S = myStructType(ui);
            my_FragColor = vec4(0, 1, 0, 1);
        })";
    compile(shaderString);
    ASSERT_TRUE(notFoundInCode("myStructType"));
}