1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "clparser.h"
16
17 #include "test.h"
18 #include "util.h"
19
TEST(CLParserTest,ShowIncludes)20 TEST(CLParserTest, ShowIncludes) {
21 ASSERT_EQ("", CLParser::FilterShowIncludes("", ""));
22
23 ASSERT_EQ("", CLParser::FilterShowIncludes("Sample compiler output", ""));
24 ASSERT_EQ("c:\\Some Files\\foobar.h",
25 CLParser::FilterShowIncludes("Note: including file: "
26 "c:\\Some Files\\foobar.h", ""));
27 ASSERT_EQ("c:\\initspaces.h",
28 CLParser::FilterShowIncludes("Note: including file: "
29 "c:\\initspaces.h", ""));
30 ASSERT_EQ("c:\\initspaces.h",
31 CLParser::FilterShowIncludes("Non-default prefix: inc file: "
32 "c:\\initspaces.h",
33 "Non-default prefix: inc file:"));
34 }
35
TEST(CLParserTest,FilterInputFilename)36 TEST(CLParserTest, FilterInputFilename) {
37 ASSERT_TRUE(CLParser::FilterInputFilename("foobar.cc"));
38 ASSERT_TRUE(CLParser::FilterInputFilename("foo bar.cc"));
39 ASSERT_TRUE(CLParser::FilterInputFilename("baz.c"));
40 ASSERT_TRUE(CLParser::FilterInputFilename("FOOBAR.CC"));
41
42 ASSERT_FALSE(CLParser::FilterInputFilename(
43 "src\\cl_helper.cc(166) : fatal error C1075: end "
44 "of file found ..."));
45 }
46
TEST(CLParserTest,ParseSimple)47 TEST(CLParserTest, ParseSimple) {
48 CLParser parser;
49 string output, err;
50 ASSERT_TRUE(parser.Parse(
51 "foo\r\n"
52 "Note: inc file prefix: foo.h\r\n"
53 "bar\r\n",
54 "Note: inc file prefix:", &output, &err));
55
56 ASSERT_EQ("foo\nbar\n", output);
57 ASSERT_EQ(1u, parser.includes_.size());
58 ASSERT_EQ("foo.h", *parser.includes_.begin());
59 }
60
TEST(CLParserTest,ParseFilenameFilter)61 TEST(CLParserTest, ParseFilenameFilter) {
62 CLParser parser;
63 string output, err;
64 ASSERT_TRUE(parser.Parse(
65 "foo.cc\r\n"
66 "cl: warning\r\n",
67 "", &output, &err));
68 ASSERT_EQ("cl: warning\n", output);
69 }
70
TEST(CLParserTest,ParseSystemInclude)71 TEST(CLParserTest, ParseSystemInclude) {
72 CLParser parser;
73 string output, err;
74 ASSERT_TRUE(parser.Parse(
75 "Note: including file: c:\\Program Files\\foo.h\r\n"
76 "Note: including file: d:\\Microsoft Visual Studio\\bar.h\r\n"
77 "Note: including file: path.h\r\n",
78 "", &output, &err));
79 // We should have dropped the first two includes because they look like
80 // system headers.
81 ASSERT_EQ("", output);
82 ASSERT_EQ(1u, parser.includes_.size());
83 ASSERT_EQ("path.h", *parser.includes_.begin());
84 }
85
TEST(CLParserTest,DuplicatedHeader)86 TEST(CLParserTest, DuplicatedHeader) {
87 CLParser parser;
88 string output, err;
89 ASSERT_TRUE(parser.Parse(
90 "Note: including file: foo.h\r\n"
91 "Note: including file: bar.h\r\n"
92 "Note: including file: foo.h\r\n",
93 "", &output, &err));
94 // We should have dropped one copy of foo.h.
95 ASSERT_EQ("", output);
96 ASSERT_EQ(2u, parser.includes_.size());
97 }
98
TEST(CLParserTest,DuplicatedHeaderPathConverted)99 TEST(CLParserTest, DuplicatedHeaderPathConverted) {
100 CLParser parser;
101 string output, err;
102
103 // This isn't inline in the Parse() call below because the #ifdef in
104 // a macro expansion would confuse MSVC2013's preprocessor.
105 const char kInput[] =
106 "Note: including file: sub/./foo.h\r\n"
107 "Note: including file: bar.h\r\n"
108 #ifdef _WIN32
109 "Note: including file: sub\\foo.h\r\n";
110 #else
111 "Note: including file: sub/foo.h\r\n";
112 #endif
113 ASSERT_TRUE(parser.Parse(kInput, "", &output, &err));
114 // We should have dropped one copy of foo.h.
115 ASSERT_EQ("", output);
116 ASSERT_EQ(2u, parser.includes_.size());
117 }
118