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
20using namespace std;
21
22TEST(CLParserTest, ShowIncludes) {
23 ASSERT_EQ("", CLParser::FilterShowIncludes("", ""));
24
25 ASSERT_EQ("", CLParser::FilterShowIncludes("Sample compiler output", ""));
26 ASSERT_EQ("c:\\Some Files\\foobar.h",
27 CLParser::FilterShowIncludes("Note: including file: "
28 "c:\\Some Files\\foobar.h", ""));
29 ASSERT_EQ("c:\\initspaces.h",
30 CLParser::FilterShowIncludes("Note: including file: "
31 "c:\\initspaces.h", ""));
32 ASSERT_EQ("c:\\initspaces.h",
33 CLParser::FilterShowIncludes("Non-default prefix: inc file: "
34 "c:\\initspaces.h",
35 "Non-default prefix: inc file:"));
36}
37
38TEST(CLParserTest, FilterInputFilename) {
39 ASSERT_TRUE(CLParser::FilterInputFilename("foobar.cc"));
40 ASSERT_TRUE(CLParser::FilterInputFilename("foo bar.cc"));
41 ASSERT_TRUE(CLParser::FilterInputFilename("baz.c"));
42 ASSERT_TRUE(CLParser::FilterInputFilename("FOOBAR.CC"));
43
44 ASSERT_FALSE(CLParser::FilterInputFilename(
45 "src\\cl_helper.cc(166) : fatal error C1075: end "
46 "of file found ..."));
47}
48
49TEST(CLParserTest, ParseSimple) {
50 CLParser parser;
51 string output, err;
52 ASSERT_TRUE(parser.Parse(
53 "foo\r\n"
54 "Note: inc file prefix: foo.h\r\n"
55 "bar\r\n",
56 "Note: inc file prefix:", &output, &err));
57
58 ASSERT_EQ("foo\nbar\n", output);
59 ASSERT_EQ(1u, parser.includes_.size());
60 ASSERT_EQ("foo.h", *parser.includes_.begin());
61}
62
63TEST(CLParserTest, ParseFilenameFilter) {
64 CLParser parser;
65 string output, err;
66 ASSERT_TRUE(parser.Parse(
67 "foo.cc\r\n"
68 "cl: warning\r\n",
69 "", &output, &err));
70 ASSERT_EQ("cl: warning\n", output);
71}
72
73TEST(CLParserTest, NoFilenameFilterAfterShowIncludes) {
74 CLParser parser;
75 string output, err;
76 ASSERT_TRUE(parser.Parse(
77 "foo.cc\r\n"
78 "Note: including file: foo.h\r\n"
79 "something something foo.cc\r\n",
80 "", &output, &err));
81 ASSERT_EQ("something something foo.cc\n", output);
82}
83
84TEST(CLParserTest, ParseSystemInclude) {
85 CLParser parser;
86 string output, err;
87 ASSERT_TRUE(parser.Parse(
88 "Note: including file: c:\\Program Files\\foo.h\r\n"
89 "Note: including file: d:\\Microsoft Visual Studio\\bar.h\r\n"
90 "Note: including file: path.h\r\n",
91 "", &output, &err));
92 // We should have dropped the first two includes because they look like
93 // system headers.
94 ASSERT_EQ("", output);
95 ASSERT_EQ(1u, parser.includes_.size());
96 ASSERT_EQ("path.h", *parser.includes_.begin());
97}
98
99TEST(CLParserTest, DuplicatedHeader) {
100 CLParser parser;
101 string output, err;
102 ASSERT_TRUE(parser.Parse(
103 "Note: including file: foo.h\r\n"
104 "Note: including file: bar.h\r\n"
105 "Note: including file: foo.h\r\n",
106 "", &output, &err));
107 // We should have dropped one copy of foo.h.
108 ASSERT_EQ("", output);
109 ASSERT_EQ(2u, parser.includes_.size());
110}
111
112TEST(CLParserTest, DuplicatedHeaderPathConverted) {
113 CLParser parser;
114 string output, err;
115
116 // This isn't inline in the Parse() call below because the #ifdef in
117 // a macro expansion would confuse MSVC2013's preprocessor.
118 const char kInput[] =
119 "Note: including file: sub/./foo.h\r\n"
120 "Note: including file: bar.h\r\n"
121#ifdef _WIN32
122 "Note: including file: sub\\foo.h\r\n";
123#else
124 "Note: including file: sub/foo.h\r\n";
125#endif
126 ASSERT_TRUE(parser.Parse(kInput, "", &output, &err));
127 // We should have dropped one copy of foo.h.
128 ASSERT_EQ("", output);
129 ASSERT_EQ(2u, parser.includes_.size());
130}
131