1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/c_include_iterator.h"
7 #include "tools/gn/input_file.h"
8 #include "tools/gn/location.h"
12 bool RangeIs(const LocationRange
& range
,
13 int line
, int begin_char
, int end_char
) {
14 return range
.begin().line_number() == line
&&
15 range
.end().line_number() == line
&&
16 range
.begin().char_offset() == begin_char
&&
17 range
.end().char_offset() == end_char
;
22 TEST(CIncludeIterator
, Basic
) {
24 buffer
.append("// Some comment\n");
26 buffer
.append("#include \"foo/bar.h\"\n");
28 buffer
.append("#include <stdio.h>\n");
30 buffer
.append(" #include \"foo/baz.h\"\n"); // Leading whitespace
31 buffer
.append("#include \"la/deda.h\"\n");
32 // Line annotated with "// nogncheck"
33 buffer
.append("#include \"should_be_skipped.h\" // nogncheck\n");
34 buffer
.append("#import \"weird_mac_import.h\"\n");
36 buffer
.append("void SomeCode() {\n");
38 InputFile
file(SourceFile("//foo.cc"));
39 file
.SetContents(buffer
);
41 CIncludeIterator
iter(&file
);
43 base::StringPiece contents
;
45 EXPECT_TRUE(iter
.GetNextIncludeString(&contents
, &range
));
46 EXPECT_EQ("foo/bar.h", contents
);
47 EXPECT_TRUE(RangeIs(range
, 3, 11, 20)) << range
.begin().Describe(true);
49 EXPECT_TRUE(iter
.GetNextIncludeString(&contents
, &range
));
50 EXPECT_EQ("foo/baz.h", contents
);
51 EXPECT_TRUE(RangeIs(range
, 7, 12, 21)) << range
.begin().Describe(true);
53 EXPECT_TRUE(iter
.GetNextIncludeString(&contents
, &range
));
54 EXPECT_EQ("la/deda.h", contents
);
55 EXPECT_TRUE(RangeIs(range
, 8, 11, 20)) << range
.begin().Describe(true);
57 // The line annotated with "nogncheck" should be skipped.
59 EXPECT_TRUE(iter
.GetNextIncludeString(&contents
, &range
));
60 EXPECT_EQ("weird_mac_import.h", contents
);
61 EXPECT_TRUE(RangeIs(range
, 10, 10, 28)) << range
.begin().Describe(true);
63 EXPECT_FALSE(iter
.GetNextIncludeString(&contents
, &range
));
66 // Tests that we don't search for includes indefinitely.
67 TEST(CIncludeIterator
, GiveUp
) {
69 for (size_t i
= 0; i
< 1000; i
++)
71 buffer
.append("#include \"foo/bar.h\"\n");
73 InputFile
file(SourceFile("//foo.cc"));
74 file
.SetContents(buffer
);
76 base::StringPiece contents
;
79 CIncludeIterator
iter(&file
);
80 EXPECT_FALSE(iter
.GetNextIncludeString(&contents
, &range
));
81 EXPECT_TRUE(contents
.empty());
84 // Don't count blank lines, comments, and preprocessor when giving up.
85 TEST(CIncludeIterator
, DontGiveUp
) {
87 for (size_t i
= 0; i
< 1000; i
++)
88 buffer
.push_back('\n');
89 for (size_t i
= 0; i
< 1000; i
++)
90 buffer
.append("// comment\n");
91 for (size_t i
= 0; i
< 1000; i
++)
92 buffer
.append("#preproc\n");
93 buffer
.append("#include \"foo/bar.h\"\n");
95 InputFile
file(SourceFile("//foo.cc"));
96 file
.SetContents(buffer
);
98 base::StringPiece contents
;
101 CIncludeIterator
iter(&file
);
102 EXPECT_TRUE(iter
.GetNextIncludeString(&contents
, &range
));
103 EXPECT_EQ("foo/bar.h", contents
);
106 // Tests that we'll tolerate some small numbers of non-includes interspersed
107 // with real includes.
108 TEST(CIncludeIterator
, TolerateNonIncludes
) {
109 const size_t kSkip
= CIncludeIterator::kMaxNonIncludeLines
- 2;
110 const size_t kGroupCount
= 100;
112 std::string
include("foo/bar.h");
114 // Allow a series of includes with blanks in between.
116 for (size_t group
= 0; group
< kGroupCount
; group
++) {
117 for (size_t i
= 0; i
< kSkip
; i
++)
118 buffer
.append("foo\n");
119 buffer
.append("#include \"" + include
+ "\"\n");
122 InputFile
file(SourceFile("//foo.cc"));
123 file
.SetContents(buffer
);
125 base::StringPiece contents
;
128 CIncludeIterator
iter(&file
);
129 for (size_t group
= 0; group
< kGroupCount
; group
++) {
130 EXPECT_TRUE(iter
.GetNextIncludeString(&contents
, &range
));
131 EXPECT_EQ(include
, contents
.as_string());
133 EXPECT_FALSE(iter
.GetNextIncludeString(&contents
, &range
));