Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- HeaderIncludes.cpp - Insert/Delete #includes --*- C++ -*----------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "clang/Tooling/Inclusions/HeaderIncludes.h"
10
#include "clang/Basic/SourceManager.h"
11
#include "clang/Lex/Lexer.h"
12
#include "llvm/ADT/Optional.h"
13
#include "llvm/Support/FormatVariadic.h"
14
15
namespace clang {
16
namespace tooling {
17
namespace {
18
19
162
LangOptions createLangOpts() {
20
162
  LangOptions LangOpts;
21
162
  LangOpts.CPlusPlus = 1;
22
162
  LangOpts.CPlusPlus11 = 1;
23
162
  LangOpts.CPlusPlus14 = 1;
24
162
  LangOpts.LineComment = 1;
25
162
  LangOpts.CXXOperatorNames = 1;
26
162
  LangOpts.Bool = 1;
27
162
  LangOpts.ObjC = 1;
28
162
  LangOpts.MicrosoftExt = 1;    // To get kw___try, kw___finally.
29
162
  LangOpts.DeclSpecKeyword = 1; // To get __declspec.
30
162
  LangOpts.WChar = 1;           // To get wchar_t
31
162
  return LangOpts;
32
162
}
33
34
// Returns the offset after skipping a sequence of tokens, matched by \p
35
// GetOffsetAfterSequence, from the start of the code.
36
// \p GetOffsetAfterSequence should be a function that matches a sequence of
37
// tokens and returns an offset after the sequence.
38
unsigned getOffsetAfterTokenSequence(
39
    StringRef FileName, StringRef Code, const IncludeStyle &Style,
40
    llvm::function_ref<unsigned(const SourceManager &, Lexer &, Token &)>
41
162
        GetOffsetAfterSequence) {
42
162
  SourceManagerForFile VirtualSM(FileName, Code);
43
162
  SourceManager &SM = VirtualSM.get();
44
162
  Lexer Lex(SM.getMainFileID(), SM.getBuffer(SM.getMainFileID()), SM,
45
162
            createLangOpts());
46
162
  Token Tok;
47
162
  // Get the first token.
48
162
  Lex.LexFromRawLexer(Tok);
49
162
  return GetOffsetAfterSequence(SM, Lex, Tok);
50
162
}
51
52
// Check if a sequence of tokens is like "#<Name> <raw_identifier>". If it is,
53
// \p Tok will be the token after this directive; otherwise, it can be any token
54
// after the given \p Tok (including \p Tok). If \p RawIDName is provided, the
55
// (second) raw_identifier name is checked.
56
bool checkAndConsumeDirectiveWithName(
57
    Lexer &Lex, StringRef Name, Token &Tok,
58
115
    llvm::Optional<StringRef> RawIDName = llvm::None) {
59
115
  bool Matched = Tok.is(tok::hash) && 
!Lex.LexFromRawLexer(Tok)92
&&
60
115
                 
Tok.is(tok::raw_identifier)92
&&
61
115
                 
Tok.getRawIdentifier() == Name92
&&
!Lex.LexFromRawLexer(Tok)14
&&
62
115
                 
Tok.is(tok::raw_identifier)14
&&
63
115
                 
(13
!RawIDName13
||
Tok.getRawIdentifier() == *RawIDName1
);
64
115
  if (Matched)
65
13
    Lex.LexFromRawLexer(Tok);
66
115
  return Matched;
67
115
}
68
69
169
void skipComments(Lexer &Lex, Token &Tok) {
70
169
  while (Tok.is(tok::comment))
71
0
    if (Lex.LexFromRawLexer(Tok))
72
0
      return;
73
169
}
74
75
// Returns the offset after header guard directives and any comments
76
// before/after header guards (e.g. #ifndef/#define pair, #pragma once). If no
77
// header guard is present in the code, this will return the offset after
78
// skipping all comments from the start of the code.
79
unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName,
80
                                               StringRef Code,
81
54
                                               const IncludeStyle &Style) {
82
54
  // \p Consume returns location after header guard or 0 if no header guard is
83
54
  // found.
84
54
  auto ConsumeHeaderGuardAndComment =
85
54
      [&](std::function<unsigned(const SourceManager &SM, Lexer &Lex,
86
54
                                 Token Tok)>
87
108
              Consume) {
88
108
        return getOffsetAfterTokenSequence(
89
108
            FileName, Code, Style,
90
108
            [&Consume](const SourceManager &SM, Lexer &Lex, Token Tok) {
91
108
              skipComments(Lex, Tok);
92
108
              unsigned InitialOffset = SM.getFileOffset(Tok.getLocation());
93
108
              return std::max(InitialOffset, Consume(SM, Lex, Tok));
94
108
            });
95
108
      };
96
54
  return std::max(
97
54
      // #ifndef/#define
98
54
      ConsumeHeaderGuardAndComment(
99
54
          [](const SourceManager &SM, Lexer &Lex, Token Tok) -> unsigned {
100
54
            if (checkAndConsumeDirectiveWithName(Lex, "ifndef", Tok)) {
101
7
              skipComments(Lex, Tok);
102
7
              if (checkAndConsumeDirectiveWithName(Lex, "define", Tok))
103
5
                return SM.getFileOffset(Tok.getLocation());
104
49
            }
105
49
            return 0;
106
49
          }),
107
54
      // #pragma once
108
54
      ConsumeHeaderGuardAndComment(
109
54
          [](const SourceManager &SM, Lexer &Lex, Token Tok) -> unsigned {
110
54
            if (checkAndConsumeDirectiveWithName(Lex, "pragma", Tok,
111
54
                                                 StringRef("once")))
112
1
              return SM.getFileOffset(Tok.getLocation());
113
53
            return 0;
114
53
          }));
115
54
}
116
117
// Check if a sequence of tokens is like
118
//    "#include ("header.h" | <header.h>)".
119
// If it is, \p Tok will be the token after this directive; otherwise, it can be
120
// any token after the given \p Tok (including \p Tok).
121
139
bool checkAndConsumeInclusiveDirective(Lexer &Lex, Token &Tok) {
122
139
  auto Matched = [&]() {
123
85
    Lex.LexFromRawLexer(Tok);
124
85
    return true;
125
85
  };
126
139
  if (Tok.is(tok::hash) && 
!Lex.LexFromRawLexer(Tok)89
&&
127
139
      
Tok.is(tok::raw_identifier)89
&&
Tok.getRawIdentifier() == "include"89
) {
128
85
    if (Lex.LexFromRawLexer(Tok))
129
0
      return false;
130
85
    if (Tok.is(tok::string_literal))
131
59
      return Matched();
132
26
    if (Tok.is(tok::less)) {
133
66
      while (!Lex.LexFromRawLexer(Tok) && 
Tok.isNot(tok::greater)64
) {
134
40
      }
135
26
      if (Tok.is(tok::greater))
136
26
        return Matched();
137
54
    }
138
26
  }
139
54
  return false;
140
54
}
141
142
// Returns the offset of the last #include directive after which a new
143
// #include can be inserted. This ignores #include's after the #include block(s)
144
// in the beginning of a file to avoid inserting headers into code sections
145
// where new #include's should not be added by default.
146
// These code sections include:
147
//      - raw string literals (containing #include).
148
//      - #if blocks.
149
//      - Special #include's among declarations (e.g. functions).
150
//
151
// If no #include after which a new #include can be inserted, this returns the
152
// offset after skipping all comments from the start of the code.
153
// Inserting after an #include is not allowed if it comes after code that is not
154
// #include (e.g. pre-processing directive that is not #include, declarations).
155
unsigned getMaxHeaderInsertionOffset(StringRef FileName, StringRef Code,
156
54
                                     const IncludeStyle &Style) {
157
54
  return getOffsetAfterTokenSequence(
158
54
      FileName, Code, Style,
159
54
      [](const SourceManager &SM, Lexer &Lex, Token Tok) {
160
54
        skipComments(Lex, Tok);
161
54
        unsigned MaxOffset = SM.getFileOffset(Tok.getLocation());
162
139
        while (checkAndConsumeInclusiveDirective(Lex, Tok))
163
85
          MaxOffset = SM.getFileOffset(Tok.getLocation());
164
54
        return MaxOffset;
165
54
      });
166
54
}
167
168
167
inline StringRef trimInclude(StringRef IncludeName) {
169
167
  return IncludeName.trim("\"<>");
170
167
}
171
172
const char IncludeRegexPattern[] =
173
    R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))";
174
175
} // anonymous namespace
176
177
IncludeCategoryManager::IncludeCategoryManager(const IncludeStyle &Style,
178
                                               StringRef FileName)
179
1.01k
    : Style(Style), FileName(FileName) {
180
1.01k
  FileStem = llvm::sys::path::stem(FileName);
181
1.01k
  for (const auto &Category : Style.IncludeCategories)
182
3.06k
    CategoryRegexs.emplace_back(Category.Regex, llvm::Regex::IgnoreCase);
183
1.01k
  IsMainFile = FileName.endswith(".c") || FileName.endswith(".cc") ||
184
1.01k
               
FileName.endswith(".cpp")438
||
FileName.endswith(".c++")258
||
185
1.01k
               
FileName.endswith(".cxx")258
||
FileName.endswith(".m")258
||
186
1.01k
               
FileName.endswith(".mm")258
;
187
1.01k
  if (!Style.IncludeIsMainSourceRegex.empty()) {
188
4
    llvm::Regex MainFileRegex(Style.IncludeIsMainSourceRegex);
189
4
    IsMainFile |= MainFileRegex.match(FileName);
190
4
  }
191
1.01k
}
192
193
int IncludeCategoryManager::getIncludePriority(StringRef IncludeName,
194
732
                                               bool CheckMainHeader) const {
195
732
  int Ret = INT_MAX;
196
2.02k
  for (unsigned i = 0, e = CategoryRegexs.size(); i != e; 
++i1.28k
)
197
2.02k
    if (CategoryRegexs[i].match(IncludeName)) {
198
732
      Ret = Style.IncludeCategories[i].Priority;
199
732
      break;
200
732
    }
201
732
  if (CheckMainHeader && 
IsMainFile606
&&
Ret > 0583
&&
isMainHeader(IncludeName)581
)
202
27
    Ret = 0;
203
732
  return Ret;
204
732
}
205
206
int IncludeCategoryManager::getSortIncludePriority(StringRef IncludeName,
207
583
                                                   bool CheckMainHeader) const {
208
583
  int Ret = INT_MAX;
209
1.61k
  for (unsigned i = 0, e = CategoryRegexs.size(); i != e; 
++i1.03k
)
210
1.61k
    if (CategoryRegexs[i].match(IncludeName)) {
211
583
      Ret = Style.IncludeCategories[i].SortPriority;
212
583
      if (Ret == 0)
213
566
        Ret = Style.IncludeCategories[i].Priority;
214
583
      break;
215
583
    }
216
583
  if (CheckMainHeader && 
IsMainFile536
&&
Ret > 0513
&&
isMainHeader(IncludeName)511
)
217
17
    Ret = 0;
218
583
  return Ret;
219
583
}
220
1.09k
bool IncludeCategoryManager::isMainHeader(StringRef IncludeName) const {
221
1.09k
  if (!IncludeName.startswith("\""))
222
349
    return false;
223
743
  StringRef HeaderStem =
224
743
      llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1));
225
743
  if (FileStem.startswith(HeaderStem) ||
226
743
      
FileStem.startswith_lower(HeaderStem)699
) {
227
46
    llvm::Regex MainIncludeRegex(HeaderStem.str() + Style.IncludeIsMainRegex,
228
46
                                 llvm::Regex::IgnoreCase);
229
46
    if (MainIncludeRegex.match(FileStem))
230
44
      return true;
231
699
  }
232
699
  return false;
233
699
}
234
235
HeaderIncludes::HeaderIncludes(StringRef FileName, StringRef Code,
236
                               const IncludeStyle &Style)
237
    : FileName(FileName), Code(Code), FirstIncludeOffset(-1),
238
      MinInsertOffset(
239
          getOffsetAfterHeaderGuardsAndComments(FileName, Code, Style)),
240
      MaxInsertOffset(MinInsertOffset +
241
                      getMaxHeaderInsertionOffset(
242
                          FileName, Code.drop_front(MinInsertOffset), Style)),
243
      Categories(Style, FileName),
244
54
      IncludeRegex(llvm::Regex(IncludeRegexPattern)) {
245
54
  // Add 0 for main header and INT_MAX for headers that are not in any
246
54
  // category.
247
54
  Priorities = {0, INT_MAX};
248
54
  for (const auto &Category : Style.IncludeCategories)
249
168
    Priorities.insert(Category.Priority);
250
54
  SmallVector<StringRef, 32> Lines;
251
54
  Code.drop_front(MinInsertOffset).split(Lines, "\n");
252
54
253
54
  unsigned Offset = MinInsertOffset;
254
54
  unsigned NextLineOffset;
255
54
  SmallVector<StringRef, 4> Matches;
256
204
  for (auto Line : Lines) {
257
204
    NextLineOffset = std::min(Code.size(), Offset + Line.size() + 1);
258
204
    if (IncludeRegex.match(Line, &Matches)) {
259
89
      // If this is the last line without trailing newline, we need to make
260
89
      // sure we don't delete across the file boundary.
261
89
      addExistingInclude(
262
89
          Include(Matches[2],
263
89
                  tooling::Range(
264
89
                      Offset, std::min(Line.size() + 1, Code.size() - Offset))),
265
89
          NextLineOffset);
266
89
    }
267
204
    Offset = NextLineOffset;
268
204
  }
269
54
270
54
  // Populate CategoryEndOfssets:
271
54
  // - Ensure that CategoryEndOffset[Highest] is always populated.
272
54
  // - If CategoryEndOffset[Priority] isn't set, use the next higher value
273
54
  // that is set, up to CategoryEndOffset[Highest].
274
54
  auto Highest = Priorities.begin();
275
54
  if (CategoryEndOffsets.find(*Highest) == CategoryEndOffsets.end()) {
276
46
    if (FirstIncludeOffset >= 0)
277
28
      CategoryEndOffsets[*Highest] = FirstIncludeOffset;
278
18
    else
279
18
      CategoryEndOffsets[*Highest] = MinInsertOffset;
280
46
  }
281
54
  // By this point, CategoryEndOffset[Highest] is always set appropriately:
282
54
  //  - to an appropriate location before/after existing #includes, or
283
54
  //  - to right after the header guard, or
284
54
  //  - to the beginning of the file.
285
270
  for (auto I = ++Priorities.begin(), E = Priorities.end(); I != E; 
++I216
)
286
216
    if (CategoryEndOffsets.find(*I) == CategoryEndOffsets.end())
287
164
      CategoryEndOffsets[*I] = CategoryEndOffsets[*std::prev(I)];
288
54
}
289
290
// \p Offset: the start of the line following this include directive.
291
void HeaderIncludes::addExistingInclude(Include IncludeToAdd,
292
89
                                        unsigned NextLineOffset) {
293
89
  auto Iter =
294
89
      ExistingIncludes.try_emplace(trimInclude(IncludeToAdd.Name)).first;
295
89
  Iter->second.push_back(std::move(IncludeToAdd));
296
89
  auto &CurInclude = Iter->second.back();
297
89
  // The header name with quotes or angle brackets.
298
89
  // Only record the offset of current #include if we can insert after it.
299
89
  if (CurInclude.R.getOffset() <= MaxInsertOffset) {
300
85
    int Priority = Categories.getIncludePriority(
301
85
        CurInclude.Name, /*CheckMainHeader=*/FirstIncludeOffset < 0);
302
85
    CategoryEndOffsets[Priority] = NextLineOffset;
303
85
    IncludesByPriority[Priority].push_back(&CurInclude);
304
85
    if (FirstIncludeOffset < 0)
305
36
      FirstIncludeOffset = CurInclude.R.getOffset();
306
85
  }
307
89
}
308
309
llvm::Optional<tooling::Replacement>
310
67
HeaderIncludes::insert(llvm::StringRef IncludeName, bool IsAngled) const {
311
67
  assert(IncludeName == trimInclude(IncludeName));
312
67
  // If a <header> ("header") already exists in code, "header" (<header>) with
313
67
  // different quotation will still be inserted.
314
67
  // FIXME: figure out if this is the best behavior.
315
67
  auto It = ExistingIncludes.find(IncludeName);
316
67
  if (It != ExistingIncludes.end())
317
4
    for (const auto &Inc : It->second)
318
4
      if ((IsAngled && 
StringRef(Inc.Name).startswith("<")2
) ||
319
4
          
(2
!IsAngled2
&&
StringRef(Inc.Name).startswith("\"")2
))
320
3
        return llvm::None;
321
67
  std::string Quoted =
322
64
      std::string(llvm::formatv(IsAngled ? 
"<{0}>"31
:
"\"{0}\""33
, IncludeName));
323
64
  StringRef QuotedName = Quoted;
324
64
  int Priority = Categories.getIncludePriority(
325
64
      QuotedName, /*CheckMainHeader=*/FirstIncludeOffset < 0);
326
64
  auto CatOffset = CategoryEndOffsets.find(Priority);
327
64
  assert(CatOffset != CategoryEndOffsets.end());
328
64
  unsigned InsertOffset = CatOffset->second; // Fall back offset
329
64
  auto Iter = IncludesByPriority.find(Priority);
330
64
  if (Iter != IncludesByPriority.end()) {
331
32
    for (const auto *Inc : Iter->second) {
332
32
      if (QuotedName < Inc->Name) {
333
11
        InsertOffset = Inc->R.getOffset();
334
11
        break;
335
11
      }
336
32
    }
337
22
  }
338
64
  assert(InsertOffset <= Code.size());
339
64
  std::string NewInclude =
340
64
      std::string(llvm::formatv("#include {0}\n", QuotedName));
341
64
  // When inserting headers at end of the code, also append '\n' to the code
342
64
  // if it does not end with '\n'.
343
64
  // FIXME: when inserting multiple #includes at the end of code, only one
344
64
  // newline should be added.
345
64
  if (InsertOffset == Code.size() && 
(10
!Code.empty()10
&&
Code.back() != '\n'9
))
346
3
    NewInclude = "\n" + NewInclude;
347
64
  return tooling::Replacement(FileName, InsertOffset, 0, NewInclude);
348
67
}
349
350
tooling::Replacements HeaderIncludes::remove(llvm::StringRef IncludeName,
351
11
                                             bool IsAngled) const {
352
11
  assert(IncludeName == trimInclude(IncludeName));
353
11
  tooling::Replacements Result;
354
11
  auto Iter = ExistingIncludes.find(IncludeName);
355
11
  if (Iter == ExistingIncludes.end())
356
0
    return Result;
357
13
  
for (const auto &Inc : Iter->second)11
{
358
13
    if ((IsAngled && 
StringRef(Inc.Name).startswith("\"")4
) ||
359
13
        
(12
!IsAngled12
&&
StringRef(Inc.Name).startswith("<")9
))
360
2
      continue;
361
11
    llvm::Error Err = Result.add(tooling::Replacement(
362
11
        FileName, Inc.R.getOffset(), Inc.R.getLength(), ""));
363
11
    if (Err) {
364
0
      auto ErrMsg = "Unexpected conflicts in #include deletions: " +
365
0
                    llvm::toString(std::move(Err));
366
0
      llvm_unreachable(ErrMsg.c_str());
367
0
    }
368
11
  }
369
11
  return Result;
370
11
}
371
372
} // namespace tooling
373
} // namespace clang