Path: blob/main_old/src/tests/preprocessor_tests/if_test.cpp
1693 views
//1// Copyright 2012 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56#include "PreprocessorTest.h"7#include "compiler/preprocessor/Token.h"89namespace angle10{1112class IfTest : public SimplePreprocessorTest13{};1415TEST_F(IfTest, If_0)16{17const char *str =18"pass_1\n"19"#if 0\n"20"fail\n"21"#endif\n"22"pass_2\n";23const char *expected =24"pass_1\n"25"\n"26"\n"27"\n"28"pass_2\n";2930preprocess(str, expected);31}3233TEST_F(IfTest, If_1)34{35const char *str =36"pass_1\n"37"#if 1\n"38"pass_2\n"39"#endif\n"40"pass_3\n";41const char *expected =42"pass_1\n"43"\n"44"pass_2\n"45"\n"46"pass_3\n";4748preprocess(str, expected);49}5051TEST_F(IfTest, If_0_Else)52{53const char *str =54"pass_1\n"55"#if 0\n"56"fail\n"57"#else\n"58"pass_2\n"59"#endif\n"60"pass_3\n";61const char *expected =62"pass_1\n"63"\n"64"\n"65"\n"66"pass_2\n"67"\n"68"pass_3\n";6970preprocess(str, expected);71}7273TEST_F(IfTest, If_1_Else)74{75const char *str =76"pass_1\n"77"#if 1\n"78"pass_2\n"79"#else\n"80"fail\n"81"#endif\n"82"pass_3\n";83const char *expected =84"pass_1\n"85"\n"86"pass_2\n"87"\n"88"\n"89"\n"90"pass_3\n";9192preprocess(str, expected);93}9495TEST_F(IfTest, If_0_Elif)96{97const char *str =98"pass_1\n"99"#if 0\n"100"fail_1\n"101"#elif 0\n"102"fail_2\n"103"#elif 1\n"104"pass_2\n"105"#elif 1\n"106"fail_3\n"107"#else\n"108"fail_4\n"109"#endif\n"110"pass_3\n";111const char *expected =112"pass_1\n"113"\n"114"\n"115"\n"116"\n"117"\n"118"pass_2\n"119"\n"120"\n"121"\n"122"\n"123"\n"124"pass_3\n";125126preprocess(str, expected);127}128129TEST_F(IfTest, If_1_Elif)130{131const char *str =132"pass_1\n"133"#if 1\n"134"pass_2\n"135"#elif 0\n"136"fail_1\n"137"#elif 1\n"138"fail_2\n"139"#else\n"140"fail_4\n"141"#endif\n"142"pass_3\n";143const char *expected =144"pass_1\n"145"\n"146"pass_2\n"147"\n"148"\n"149"\n"150"\n"151"\n"152"\n"153"\n"154"pass_3\n";155156preprocess(str, expected);157}158159TEST_F(IfTest, If_Elif_Else)160{161const char *str =162"pass_1\n"163"#if 0\n"164"fail_1\n"165"#elif 0\n"166"fail_2\n"167"#elif 0\n"168"fail_3\n"169"#else\n"170"pass_2\n"171"#endif\n"172"pass_3\n";173const char *expected =174"pass_1\n"175"\n"176"\n"177"\n"178"\n"179"\n"180"\n"181"\n"182"pass_2\n"183"\n"184"pass_3\n";185186preprocess(str, expected);187}188189TEST_F(IfTest, If_0_Nested)190{191const char *str =192"pass_1\n"193"#if 0\n"194"fail_1\n"195"#if 1\n"196"fail_2\n"197"#else\n"198"fail_3\n"199"#endif\n"200"#else\n"201"pass_2\n"202"#endif\n"203"pass_3\n";204const char *expected =205"pass_1\n"206"\n"207"\n"208"\n"209"\n"210"\n"211"\n"212"\n"213"\n"214"pass_2\n"215"\n"216"pass_3\n";217218preprocess(str, expected);219}220221TEST_F(IfTest, If_1_Nested)222{223const char *str =224"pass_1\n"225"#if 1\n"226"pass_2\n"227"#if 1\n"228"pass_3\n"229"#else\n"230"fail_1\n"231"#endif\n"232"#else\n"233"fail_2\n"234"#endif\n"235"pass_4\n";236const char *expected =237"pass_1\n"238"\n"239"pass_2\n"240"\n"241"pass_3\n"242"\n"243"\n"244"\n"245"\n"246"\n"247"\n"248"pass_4\n";249250preprocess(str, expected);251}252253TEST_F(IfTest, OperatorPrecedence)254{255const char *str =256"#if 1 + 2 * 3 + - (26 % 17 - + 4 / 2)\n"257"fail_1\n"258"#else\n"259"pass_1\n"260"#endif\n";261const char *expected =262"\n"263"\n"264"\n"265"pass_1\n"266"\n";267268preprocess(str, expected);269}270271TEST_F(IfTest, OperatorDefined)272{273const char *str =274"#if defined foo\n"275"fail_1\n"276"#else\n"277"pass_1\n"278"#endif\n"279"#define foo\n"280"#if defined(foo)\n"281"pass_2\n"282"#else\n"283"fail_2\n"284"#endif\n"285"#undef foo\n"286"#if defined ( foo ) \n"287"fail_3\n"288"#else\n"289"pass_3\n"290"#endif\n";291const char *expected =292"\n"293"\n"294"\n"295"pass_1\n"296"\n"297"\n"298"\n"299"pass_2\n"300"\n"301"\n"302"\n"303"\n"304"\n"305"\n"306"\n"307"pass_3\n"308"\n";309310preprocess(str, expected);311}312313TEST_F(IfTest, OperatorEQ)314{315const char *str =316"#if 4 - 1 == 2 + 1\n"317"pass\n"318"#else\n"319"fail\n"320"#endif\n";321const char *expected =322"\n"323"pass\n"324"\n"325"\n"326"\n";327328preprocess(str, expected);329}330331TEST_F(IfTest, OperatorNE)332{333const char *str =334"#if 1 != 2\n"335"pass\n"336"#else\n"337"fail\n"338"#endif\n";339const char *expected =340"\n"341"pass\n"342"\n"343"\n"344"\n";345346preprocess(str, expected);347}348349TEST_F(IfTest, OperatorLess)350{351const char *str =352"#if 1 < 2\n"353"pass\n"354"#else\n"355"fail\n"356"#endif\n";357const char *expected =358"\n"359"pass\n"360"\n"361"\n"362"\n";363364preprocess(str, expected);365}366367TEST_F(IfTest, OperatorGreater)368{369const char *str =370"#if 2 > 1\n"371"pass\n"372"#else\n"373"fail\n"374"#endif\n";375const char *expected =376"\n"377"pass\n"378"\n"379"\n"380"\n";381382preprocess(str, expected);383}384385TEST_F(IfTest, OperatorLE)386{387const char *str =388"#if 1 <= 2\n"389"pass_1\n"390"#else\n"391"fail_1\n"392"#endif\n"393"#if 2 <= 2\n"394"pass_2\n"395"#else\n"396"fail_2\n"397"#endif\n";398const char *expected =399"\n"400"pass_1\n"401"\n"402"\n"403"\n"404"\n"405"pass_2\n"406"\n"407"\n"408"\n";409410preprocess(str, expected);411}412413TEST_F(IfTest, OperatorGE)414{415const char *str =416"#if 2 >= 1\n"417"pass_1\n"418"#else\n"419"fail_1\n"420"#endif\n"421"#if 2 >= 2\n"422"pass_2\n"423"#else\n"424"fail_2\n"425"#endif\n";426const char *expected =427"\n"428"pass_1\n"429"\n"430"\n"431"\n"432"\n"433"pass_2\n"434"\n"435"\n"436"\n";437438preprocess(str, expected);439}440441TEST_F(IfTest, OperatorBitwiseOR)442{443const char *str =444"#if (0xaaaaaaaa | 0x55555555) == 0xffffffff\n"445"pass\n"446"#else\n"447"fail\n"448"#endif\n";449const char *expected =450"\n"451"pass\n"452"\n"453"\n"454"\n";455456preprocess(str, expected);457}458459TEST_F(IfTest, OperatorBitwiseAND)460{461const char *str =462"#if (0xaaaaaaa & 0x5555555) == 0\n"463"pass\n"464"#else\n"465"fail\n"466"#endif\n";467const char *expected =468"\n"469"pass\n"470"\n"471"\n"472"\n";473474preprocess(str, expected);475}476477TEST_F(IfTest, OperatorBitwiseXOR)478{479const char *str =480"#if (0xaaaaaaa ^ 0x5555555) == 0xfffffff\n"481"pass\n"482"#else\n"483"fail\n"484"#endif\n";485const char *expected =486"\n"487"pass\n"488"\n"489"\n"490"\n";491492preprocess(str, expected);493}494495TEST_F(IfTest, OperatorBitwiseComplement)496{497const char *str =498"#if (~ 0xdeadbeef) == -3735928560\n"499"pass\n"500"#else\n"501"fail\n"502"#endif\n";503const char *expected =504"\n"505"pass\n"506"\n"507"\n"508"\n";509510preprocess(str, expected);511}512513TEST_F(IfTest, OperatorLeft)514{515const char *str =516"#if (1 << 12) == 4096\n"517"pass\n"518"#else\n"519"fail\n"520"#endif\n";521const char *expected =522"\n"523"pass\n"524"\n"525"\n"526"\n";527528preprocess(str, expected);529}530531TEST_F(IfTest, OperatorRight)532{533const char *str =534"#if (31762 >> 8) == 124\n"535"pass\n"536"#else\n"537"fail\n"538"#endif\n";539const char *expected =540"\n"541"pass\n"542"\n"543"\n"544"\n";545546preprocess(str, expected);547}548549TEST_F(IfTest, ExpressionWithMacros)550{551const char *str =552"#define one 1\n"553"#define two 2\n"554"#define three 3\n"555"#if one + two == three\n"556"pass\n"557"#else\n"558"fail\n"559"#endif\n";560const char *expected =561"\n"562"\n"563"\n"564"\n"565"pass\n"566"\n"567"\n"568"\n";569570preprocess(str, expected);571}572573TEST_F(IfTest, JunkInsideExcludedBlockIgnored)574{575const char *str =576"#if 0\n"577"foo !@#$%^&* .1bar\n"578"#foo\n"579"#if bar\n"580"fail\n"581"#endif\n"582"#else\n"583"pass\n"584"#endif\n";585const char *expected =586"\n"587"\n"588"\n"589"\n"590"\n"591"\n"592"\n"593"pass\n"594"\n";595596preprocess(str, expected);597}598599TEST_F(IfTest, Ifdef)600{601const char *str =602"#define foo\n"603"#ifdef foo\n"604"pass_1\n"605"#else\n"606"fail_1\n"607"#endif\n"608"#undef foo\n"609"#ifdef foo\n"610"fail_2\n"611"#else\n"612"pass_2\n"613"#endif\n";614const char *expected =615"\n"616"\n"617"pass_1\n"618"\n"619"\n"620"\n"621"\n"622"\n"623"\n"624"\n"625"pass_2\n"626"\n";627628preprocess(str, expected);629}630631TEST_F(IfTest, Ifndef)632{633const char *str =634"#define foo\n"635"#ifndef foo\n"636"fail_1\n"637"#else\n"638"pass_1\n"639"#endif\n"640"#undef foo\n"641"#ifndef foo\n"642"pass_2\n"643"#else\n"644"fail_2\n"645"#endif\n";646const char *expected =647"\n"648"\n"649"\n"650"\n"651"pass_1\n"652"\n"653"\n"654"\n"655"pass_2\n"656"\n"657"\n"658"\n";659660preprocess(str, expected);661}662663TEST_F(IfTest, MissingExpression)664{665const char *str =666"#if\n"667"#endif\n";668669EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_INVALID_EXPRESSION,670pp::SourceLocation(0, 1), "syntax error"));671672preprocess(str);673}674675TEST_F(IfTest, DivisionByZero)676{677const char *str =678"#if 1 / (3 - (1 + 2))\n"679"#endif\n";680681EXPECT_CALL(mDiagnostics,682print(pp::Diagnostics::PP_DIVISION_BY_ZERO, pp::SourceLocation(0, 1), "1 / 0"));683684preprocess(str);685}686687TEST_F(IfTest, ModuloByZero)688{689const char *str =690"#if 1 % (3 - (1 + 2))\n"691"#endif\n";692693EXPECT_CALL(mDiagnostics,694print(pp::Diagnostics::PP_DIVISION_BY_ZERO, pp::SourceLocation(0, 1), "1 % 0"));695696preprocess(str);697}698699TEST_F(IfTest, DecIntegerOverflow)700{701const char *str =702"#if 4294967296\n"703"#endif\n";704705EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_INTEGER_OVERFLOW, pp::SourceLocation(0, 1),706"4294967296"));707708preprocess(str);709}710711TEST_F(IfTest, OctIntegerOverflow)712{713const char *str =714"#if 077777777777\n"715"#endif\n";716717EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_INTEGER_OVERFLOW, pp::SourceLocation(0, 1),718"077777777777"));719720preprocess(str);721}722723TEST_F(IfTest, HexIntegerOverflow)724{725const char *str =726"#if 0xfffffffff\n"727"#endif\n";728729EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_INTEGER_OVERFLOW, pp::SourceLocation(0, 1),730"0xfffffffff"));731732preprocess(str);733}734735TEST_F(IfTest, UndefinedMacro)736{737const char *str =738"#if UNDEFINED\n"739"#endif\n";740741EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN,742pp::SourceLocation(0, 1), "UNDEFINED"));743744preprocess(str);745}746747TEST_F(IfTest, InvalidExpressionIgnoredForExcludedElif)748{749const char *str =750"#if 1\n"751"pass\n"752"#elif UNDEFINED\n"753"fail\n"754"#endif\n";755const char *expected =756"\n"757"pass\n"758"\n"759"\n"760"\n";761762// No error or warning.763using testing::_;764EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0);765766preprocess(str, expected);767}768769TEST_F(IfTest, ElseWithoutIf)770{771const char *str = "#else\n";772773EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_CONDITIONAL_ELSE_WITHOUT_IF,774pp::SourceLocation(0, 1), "else"));775776preprocess(str);777}778779TEST_F(IfTest, ElifWithoutIf)780{781const char *str = "#elif 1\n";782783EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_CONDITIONAL_ELIF_WITHOUT_IF,784pp::SourceLocation(0, 1), "elif"));785786preprocess(str);787}788789TEST_F(IfTest, EndifWithoutIf)790{791const char *str = "#endif\n";792793EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_CONDITIONAL_ENDIF_WITHOUT_IF,794pp::SourceLocation(0, 1), "endif"));795796preprocess(str);797}798799TEST_F(IfTest, ElseAfterElse)800{801const char *str =802"#if 1\n"803"#else\n"804"#else\n"805"#endif\n";806807EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_CONDITIONAL_ELSE_AFTER_ELSE,808pp::SourceLocation(0, 3), "else"));809810preprocess(str);811}812813TEST_F(IfTest, ElifAfterElse)814{815const char *str =816"#if 1\n"817"#else\n"818"#elif 0\n"819"#endif\n";820821EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_CONDITIONAL_ELIF_AFTER_ELSE,822pp::SourceLocation(0, 3), "elif"));823824preprocess(str);825}826827TEST_F(IfTest, UnterminatedIf)828{829const char *str = "#if 1\n";830831EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_CONDITIONAL_UNTERMINATED,832pp::SourceLocation(0, 1), "if"));833834preprocess(str);835}836837TEST_F(IfTest, UnterminatedIfdef)838{839const char *str = "#ifdef foo\n";840841EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_CONDITIONAL_UNTERMINATED,842pp::SourceLocation(0, 1), "ifdef"));843844preprocess(str);845}846847// The preprocessor only allows one expression to follow an #if directive.848// Supplying two integer expressions should be an error.849TEST_F(IfTest, ExtraIntExpression)850{851const char *str =852"#if 1 1\n"853"#endif\n";854855EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN,856pp::SourceLocation(0, 1), "1"));857858preprocess(str);859}860861// The preprocessor only allows one expression to follow an #if directive.862// Supplying two expressions where one uses a preprocessor define should be an error.863TEST_F(IfTest, ExtraIdentifierExpression)864{865const char *str =866"#define one 1\n"867"#if 1 one\n"868"#endif\n";869870EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN,871pp::SourceLocation(0, 2), "1"));872873preprocess(str);874}875876// Divide by zero that's not evaluated because of short-circuiting should not cause an error.877TEST_F(IfTest, ShortCircuitedDivideByZero)878{879const char *str =880"#if 1 || (2 / 0)\n"881"pass\n"882"#endif\n";883const char *expected =884"\n"885"pass\n"886"\n";887888preprocess(str, expected);889}890891// Undefined identifier that's not evaluated because of short-circuiting should not cause an error.892TEST_F(IfTest, ShortCircuitedUndefined)893{894const char *str =895"#if 1 || UNDEFINED\n"896"pass\n"897"#endif\n";898const char *expected =899"\n"900"pass\n"901"\n";902903preprocess(str, expected);904}905906// Defined operator produced by macro expansion has undefined behavior according to C++ spec,907// which the GLSL spec references (see C++14 draft spec section 16.1.4), but this behavior is908// needed for passing dEQP tests, which enforce stricter compatibility between implementations.909TEST_F(IfTest, DefinedOperatorValidAfterMacroExpansion)910{911const char *str =912"#define foo defined\n"913"#if !foo bar\n"914"pass\n"915"#endif\n";916917preprocess(str, "\n\npass\n\n");918}919920// Validate the defined operator is evaluated when the macro is called, not when defined.921TEST_F(IfTest, DefinedOperatorValidWhenUsed)922{923constexpr char str[] = R"(#define BBB 1924#define AAA defined(BBB)925#undef BBB926927#if !AAA928pass929#endif930)";931932preprocess(str, "\n\n\n\n\npass\n\n");933}934935// Validate the defined operator is evaluated when the macro is called, not when defined.936TEST_F(IfTest, DefinedOperatorAfterMacro)937{938constexpr char str[] = R"(#define AAA defined(BBB)939#define BBB 1940941#if AAA942pass943#endif944)";945946preprocess(str, "\n\n\n\npass\n\n");947}948949// Test generating "defined" by concatenation when a macro is called. This is not allowed.950TEST_F(IfTest, DefinedInMacroConcatenationNotAllowed)951{952constexpr char str[] = R"(#define BBB 1953#define AAA(defi, ned) defi ## ned(BBB)954955#if !AAA(defi, ned)956pass957#endif958)";959960EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN,961pp::SourceLocation(0, 4), "defi"));962EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN,963pp::SourceLocation(0, 4), "#"));964965preprocess(str);966}967968// Test using defined in a macro parameter name. This is not allowed.969TEST_F(IfTest, DefinedAsParameterNameNotAllowed)970{971constexpr char str[] = R"(#define BBB 1972#define AAA(defined) defined(BBB)973974#if AAA(defined)975pass976#endif977)";978979EXPECT_CALL(mDiagnostics,980print(pp::Diagnostics::PP_UNEXPECTED_TOKEN, pp::SourceLocation(0, 0), ""));981982preprocess(str);983}984985// This behavour is disabled in WebGL.986TEST_F(IfTest, DefinedOperatorInvalidAfterMacroExpansionInWebGL)987{988const char *str =989"#define foo defined\n"990"#if !foo bar\n"991"pass\n"992"#endif\n";993994EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN,995pp::SourceLocation(0, 2), "defined"));996EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_CONDITIONAL_UNEXPECTED_TOKEN,997pp::SourceLocation(0, 2), "bar"));998999preprocess(str, pp::PreprocessorSettings(SH_WEBGL_SPEC));1000}10011002// Defined operator produced by macro expansion has undefined behavior according to C++ spec,1003// which the GLSL spec references (see C++14 draft spec section 16.1.4). Some edge case1004// behaviours with defined are not portable between implementations and thus are not required1005// to pass dEQP Tests.1006TEST_F(IfTest, UnterminatedDefinedInMacro)1007{1008const char *str =1009"#define foo defined(\n"1010"#if foo\n"1011"#endif\n";10121013EXPECT_CALL(mDiagnostics,1014print(pp::Diagnostics::PP_UNEXPECTED_TOKEN, pp::SourceLocation(0, 2), "\n"));1015EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_INVALID_EXPRESSION,1016pp::SourceLocation(0, 2), "syntax error"));10171018preprocess(str);1019}10201021// Defined operator produced by macro expansion has undefined behavior according to C++ spec,1022// which the GLSL spec references (see C++14 draft spec section 16.1.4). Some edge case1023// behaviours with defined are not portable between implementations and thus are not required1024// to pass dEQP Tests.1025TEST_F(IfTest, UnterminatedDefinedInMacro2)1026{1027const char *str =1028"#define foo defined(bar\n"1029"#if foo\n"1030"#endif\n";10311032EXPECT_CALL(mDiagnostics,1033print(pp::Diagnostics::PP_UNEXPECTED_TOKEN, pp::SourceLocation(0, 2), "\n"));1034EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_INVALID_EXPRESSION,1035pp::SourceLocation(0, 2), "syntax error"));10361037preprocess(str);1038}10391040// Undefined shift: negative shift offset.1041TEST_F(IfTest, BitShiftLeftOperatorNegativeOffset)1042{1043const char *str =1044"#if 2 << -1 == 1\n"1045"foo\n"1046"#endif\n";10471048EXPECT_CALL(mDiagnostics,1049print(pp::Diagnostics::PP_UNDEFINED_SHIFT, pp::SourceLocation(0, 1), "2 << -1"));10501051preprocess(str);1052}10531054// Undefined shift: shift offset is out of range.1055TEST_F(IfTest, BitShiftLeftOperatorOffset32)1056{1057const char *str =1058"#if 2 << 32 == 1\n"1059"foo\n"1060"#endif\n";10611062EXPECT_CALL(mDiagnostics,1063print(pp::Diagnostics::PP_UNDEFINED_SHIFT, pp::SourceLocation(0, 1), "2 << 32"));10641065preprocess(str);1066}10671068// Left hand side of shift is negative.1069TEST_F(IfTest, BitShiftLeftOperatorNegativeLHS)1070{1071const char *str =1072"#if (-2) << 1 == -4\n"1073"pass\n"1074"#endif\n";1075const char *expected =1076"\n"1077"pass\n"1078"\n";10791080preprocess(str, expected);1081}10821083// Left shift overflows. Note that the intended result is not explicitly specified, but we assume it1084// to do the same operation on the 2's complement bit representation as unsigned shift in C++.1085TEST_F(IfTest, BitShiftLeftOverflow)1086{1087const char *str =1088"#if (0x10000 + 0x1) << 28 == 0x10000000\n"1089"pass\n"1090"#endif\n";1091const char *expected =1092"\n"1093"pass\n"1094"\n";10951096preprocess(str, expected);1097}10981099// Left shift of a negative number overflows. Note that the intended result is not explicitly1100// specified, but we assume it to do the same operation on the 2's complement bit representation as1101// unsigned shift in C++.1102TEST_F(IfTest, BitShiftLeftNegativeOverflow)1103{1104// The bit representation of -5 is 11111111 11111111 11111111 11111011.1105// Shifting by 30 leaves: 11000000 00000000 00000000 00000000.1106const char *str =1107"#if (-5) << 30 == -1073741824\n"1108"pass\n"1109"#endif\n";1110const char *expected =1111"\n"1112"pass\n"1113"\n";11141115preprocess(str, expected);1116}11171118// Undefined shift: shift offset is out of range.1119TEST_F(IfTest, BitShiftRightOperatorNegativeOffset)1120{1121const char *str =1122"#if 2 >> -1 == 4\n"1123"foo\n"1124"#endif\n";11251126EXPECT_CALL(mDiagnostics,1127print(pp::Diagnostics::PP_UNDEFINED_SHIFT, pp::SourceLocation(0, 1), "2 >> -1"));11281129preprocess(str);1130}11311132// Undefined shift: shift offset is out of range.1133TEST_F(IfTest, BitShiftRightOperatorOffset32)1134{1135const char *str =1136"#if 2 >> 32 == 0\n"1137"foo\n"1138"#endif\n";11391140EXPECT_CALL(mDiagnostics,1141print(pp::Diagnostics::PP_UNDEFINED_SHIFT, pp::SourceLocation(0, 1), "2 >> 32"));11421143preprocess(str);1144}11451146// Left hand side of shift is negative.1147TEST_F(IfTest, BitShiftRightOperatorNegativeLHS)1148{1149const char *str =1150"#if (-2) >> 1 == 0x7fffffff\n"1151"pass\n"1152"#endif\n";1153const char *expected =1154"\n"1155"pass\n"1156"\n";11571158preprocess(str, expected);1159}11601161} // namespace angle116211631164