summaryrefslogtreecommitdiff
path: root/tests/preprocessor_tests/location_test.cpp
blob: c8a5f38f1b26a65dabb28b78ce3c017552fe3e44 (plain)
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
//
// Copyright (c) 2012 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.
//

#include "gtest/gtest.h"
#include "Preprocessor.h"
#include "Token.h"

static void PreprocessAndVerifyLocation(int count,
                                        const char* const string[],
                                        const int length[],
                                        pp::Token::Location location)
{
    pp::Token token;
    pp::Preprocessor preprocessor;
    ASSERT_TRUE(preprocessor.init(count, string, length));
    EXPECT_EQ(pp::Token::IDENTIFIER, preprocessor.lex(&token));
    EXPECT_EQ(pp::Token::IDENTIFIER, token.type);
    EXPECT_STREQ("foo", token.value.c_str());

    EXPECT_EQ(location.file, token.location.file);
    EXPECT_EQ(location.line, token.location.line);
}

TEST(LocationTest, String0_Line1)
{
    const char* str = "foo";
    pp::Token::Location loc;
    loc.file = 0;
    loc.line = 1;

    SCOPED_TRACE("String0_Line1");
    PreprocessAndVerifyLocation(1, &str, 0, loc);
}

TEST(LocationTest, String0_Line2)
{
    const char* str = "\nfoo";
    pp::Token::Location loc;
    loc.file = 0;
    loc.line = 2;

    SCOPED_TRACE("String0_Line2");
    PreprocessAndVerifyLocation(1, &str, 0, loc);
}

TEST(LocationTest, String1_Line1)
{
    const char* const str[] = {"\n\n", "foo"};
    pp::Token::Location loc;
    loc.file = 1;
    loc.line = 1;

    SCOPED_TRACE("String1_Line1");
    PreprocessAndVerifyLocation(2, str, 0, loc);
}

TEST(LocationTest, String1_Line2)
{
    const char* const str[] = {"\n\n", "\nfoo"};
    pp::Token::Location loc;
    loc.file = 1;
    loc.line = 2;

    SCOPED_TRACE("String1_Line2");
    PreprocessAndVerifyLocation(2, str, 0, loc);
}

TEST(LocationTest, NewlineInsideCommentCounted)
{
    const char* str = "/*\n\n*/foo";
    pp::Token::Location loc;
    loc.file = 0;
    loc.line = 3;

    SCOPED_TRACE("NewlineInsideCommentCounted");
    PreprocessAndVerifyLocation(1, &str, 0, loc);
}

TEST(LocationTest, ErrorLocationAfterComment)
{
    const char* str = "/*\n\n*/@";

    pp::Token token;
    pp::Preprocessor preprocessor;
    ASSERT_TRUE(preprocessor.init(1, &str, 0));
    EXPECT_EQ(pp::Token::INVALID_CHARACTER, preprocessor.lex(&token));
    EXPECT_EQ(pp::Token::INVALID_CHARACTER, token.type);
    EXPECT_STREQ("@", token.value.c_str());

    EXPECT_EQ(0, token.location.file);
    EXPECT_EQ(3, token.location.line);
}

// The location of a token straddling two or more strings is that of the
// first character of the token.

TEST(LocationTest, TokenStraddlingTwoStrings)
{
    const char* const str[] = {"f", "oo"};
    pp::Token::Location loc;
    loc.file = 0;
    loc.line = 1;

    SCOPED_TRACE("TokenStraddlingTwoStrings");
    PreprocessAndVerifyLocation(2, str, 0, loc);
}

TEST(LocationTest, TokenStraddlingThreeStrings)
{
    const char* const str[] = {"f", "o", "o"};
    pp::Token::Location loc;
    loc.file = 0;
    loc.line = 1;

    SCOPED_TRACE("TokenStraddlingThreeStrings");
    PreprocessAndVerifyLocation(3, str, 0, loc);
}

// TODO(alokp): Add tests for #line directives.