13#include "llvm/Support/FormatVariadic.h"
14#include "llvm/Support/Path.h"
21LangOptions createLangOpts() {
23 LangOpts.CPlusPlus = 1;
24 LangOpts.CPlusPlus11 = 1;
25 LangOpts.CPlusPlus14 = 1;
26 LangOpts.LineComment = 1;
27 LangOpts.CXXOperatorNames = 1;
30 LangOpts.MicrosoftExt = 1;
31 LangOpts.DeclSpecKeyword = 1;
40unsigned getOffsetAfterTokenSequence(
41 StringRef FileName, StringRef Code,
const IncludeStyle &Style,
42 llvm::function_ref<
unsigned(
const SourceManager &, Lexer &, Token &)>
43 GetOffsetAfterSequence) {
44 SourceManagerForFile VirtualSM(FileName, Code);
45 SourceManager &
SM = VirtualSM.get();
46 LangOptions LangOpts = createLangOpts();
47 Lexer Lex(
SM.getMainFileID(),
SM.getBufferOrFake(
SM.getMainFileID()),
SM,
51 Lex.LexFromRawLexer(Tok);
52 return GetOffsetAfterSequence(
SM, Lex, Tok);
59bool checkAndConsumeDirectiveWithName(
60 Lexer &Lex, StringRef Name, Token &Tok,
61 std::optional<StringRef> RawIDName = std::nullopt) {
62 bool Matched = Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&
63 Tok.is(tok::raw_identifier) &&
64 Tok.getRawIdentifier() == Name && !Lex.LexFromRawLexer(Tok) &&
65 Tok.is(tok::raw_identifier) &&
66 (!RawIDName || Tok.getRawIdentifier() == *RawIDName);
68 Lex.LexFromRawLexer(Tok);
72void skipComments(Lexer &Lex, Token &Tok) {
73 while (Tok.is(tok::comment))
74 if (Lex.LexFromRawLexer(Tok))
82unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName,
84 const IncludeStyle &Style) {
87 auto ConsumeHeaderGuardAndComment =
88 [&](std::function<
unsigned(
const SourceManager &
SM, Lexer &Lex,
91 return getOffsetAfterTokenSequence(
92 FileName, Code, Style,
93 [&Consume](
const SourceManager &
SM, Lexer &Lex, Token Tok) {
94 skipComments(Lex, Tok);
95 unsigned InitialOffset =
SM.getFileOffset(Tok.getLocation());
96 return std::max(InitialOffset, Consume(
SM, Lex, Tok));
101 ConsumeHeaderGuardAndComment(
102 [](
const SourceManager &
SM, Lexer &Lex, Token Tok) ->
unsigned {
103 if (checkAndConsumeDirectiveWithName(Lex,
"ifndef", Tok)) {
104 skipComments(Lex, Tok);
105 if (checkAndConsumeDirectiveWithName(Lex,
"define", Tok) &&
106 Tok.isAtStartOfLine())
107 return SM.getFileOffset(Tok.getLocation());
112 ConsumeHeaderGuardAndComment(
113 [](
const SourceManager &
SM, Lexer &Lex, Token Tok) ->
unsigned {
114 if (checkAndConsumeDirectiveWithName(Lex,
"pragma", Tok,
116 return SM.getFileOffset(Tok.getLocation());
125bool checkAndConsumeInclusiveDirective(Lexer &Lex, Token &Tok) {
126 auto Matched = [&]() {
127 Lex.LexFromRawLexer(Tok);
130 if (Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&
131 Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() ==
"include") {
132 if (Lex.LexFromRawLexer(Tok))
134 if (Tok.is(tok::string_literal))
136 if (Tok.is(tok::less)) {
137 while (!Lex.LexFromRawLexer(Tok) && Tok.isNot(tok::greater)) {
139 if (Tok.is(tok::greater))
159unsigned getMaxHeaderInsertionOffset(StringRef FileName, StringRef Code,
160 const IncludeStyle &Style) {
161 return getOffsetAfterTokenSequence(
162 FileName, Code, Style,
163 [](
const SourceManager &
SM, Lexer &Lex, Token Tok) {
164 skipComments(Lex, Tok);
165 unsigned MaxOffset =
SM.getFileOffset(Tok.getLocation());
166 while (checkAndConsumeInclusiveDirective(Lex, Tok))
172inline StringRef trimInclude(StringRef IncludeName) {
173 return IncludeName.trim(
"\"<>");
176const char IncludeRegexPattern[] =
177 R
"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))";
184StringRef matchingStem(llvm::StringRef Path) {
185 StringRef Name = llvm::sys::path::filename(Path);
186 return Name.substr(0, Name.find(
'.', 1));
193 : Style(Style), FileName(FileName) {
194 for (
const auto &
Category : Style.IncludeCategories) {
196 ? llvm::Regex::NoFlags
197 : llvm::Regex::IgnoreCase);
199 IsMainFile = FileName.endswith(
".c") || FileName.endswith(
".cc") ||
200 FileName.endswith(
".cpp") || FileName.endswith(
".c++") ||
201 FileName.endswith(
".cxx") || FileName.endswith(
".m") ||
202 FileName.endswith(
".mm");
203 if (!Style.IncludeIsMainSourceRegex.empty()) {
204 llvm::Regex MainFileRegex(Style.IncludeIsMainSourceRegex);
205 IsMainFile |= MainFileRegex.match(FileName);
210 bool CheckMainHeader)
const {
212 for (
unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i)
213 if (CategoryRegexs[i].
match(IncludeName)) {
214 Ret = Style.IncludeCategories[i].Priority;
217 if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName))
223 bool CheckMainHeader)
const {
225 for (
unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i)
226 if (CategoryRegexs[i].
match(IncludeName)) {
227 Ret = Style.IncludeCategories[i].SortPriority;
229 Ret = Style.IncludeCategories[i].Priority;
232 if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName))
236bool IncludeCategoryManager::isMainHeader(StringRef IncludeName)
const {
237 if (!IncludeName.startswith(
"\""))
241 IncludeName.drop_front(1).drop_back(1);
244 StringRef HeaderStem = llvm::sys::path::stem(IncludeName);
245 StringRef FileStem = llvm::sys::path::stem(FileName);
246 StringRef MatchingFileStem = matchingStem(FileName);
256 if (MatchingFileStem.starts_with_insensitive(HeaderStem))
257 Matching = MatchingFileStem;
258 else if (FileStem.equals_insensitive(HeaderStem))
260 if (!Matching.empty()) {
261 llvm::Regex MainIncludeRegex(HeaderStem.str() + Style.IncludeIsMainRegex,
262 llvm::Regex::IgnoreCase);
263 if (MainIncludeRegex.match(Matching))
273 : FileName(FileName), Code(Code), FirstIncludeOffset(-1),
275 getOffsetAfterHeaderGuardsAndComments(FileName, Code, Style)),
276 MaxInsertOffset(MinInsertOffset +
277 getMaxHeaderInsertionOffset(
278 FileName, Code.drop_front(MinInsertOffset), Style)),
279 MainIncludeFound(
false),
280 Categories(Style, FileName) {
284 for (
const auto &
Category : Style.IncludeCategories)
285 Priorities.insert(
Category.Priority);
287 Code.drop_front(MinInsertOffset).split(Lines,
"\n");
289 unsigned Offset = MinInsertOffset;
290 unsigned NextLineOffset;
292 for (
auto Line : Lines) {
293 NextLineOffset = std::min(Code.size(), Offset + Line.size() + 1);
300 Offset, std::min(Line.size() + 1, Code.size() - Offset)),
305 Offset = NextLineOffset;
312 auto Highest = Priorities.begin();
313 if (CategoryEndOffsets.find(*Highest) == CategoryEndOffsets.end()) {
314 if (FirstIncludeOffset >= 0)
315 CategoryEndOffsets[*Highest] = FirstIncludeOffset;
317 CategoryEndOffsets[*Highest] = MinInsertOffset;
323 for (
auto I = ++Priorities.begin(), E = Priorities.end(); I != E; ++I)
324 if (CategoryEndOffsets.find(*I) == CategoryEndOffsets.end())
325 CategoryEndOffsets[*I] = CategoryEndOffsets[*std::prev(I)];
329void HeaderIncludes::addExistingInclude(
Include IncludeToAdd,
330 unsigned NextLineOffset) {
332 ExistingIncludes.try_emplace(trimInclude(IncludeToAdd.Name)).first;
333 Iter->second.push_back(std::move(IncludeToAdd));
334 auto &CurInclude =
Iter->second.back();
337 if (CurInclude.R.getOffset() <= MaxInsertOffset) {
339 CurInclude.Name, !MainIncludeFound);
341 MainIncludeFound =
true;
342 CategoryEndOffsets[
Priority] = NextLineOffset;
343 IncludesByPriority[
Priority].push_back(&CurInclude);
344 if (FirstIncludeOffset < 0)
345 FirstIncludeOffset = CurInclude.R.getOffset();
349std::optional<tooling::Replacement>
352 assert(IncludeName == trimInclude(IncludeName));
356 auto It = ExistingIncludes.find(IncludeName);
357 if (It != ExistingIncludes.end()) {
358 for (
const auto &Inc : It->second)
360 ((IsAngled && StringRef(Inc.Name).startswith(
"<")) ||
361 (!IsAngled && StringRef(Inc.Name).startswith(
"\""))))
365 std::string(llvm::formatv(IsAngled ?
"<{0}>" :
"\"{0}\"", IncludeName));
366 StringRef QuotedName = Quoted;
368 QuotedName, !MainIncludeFound);
369 auto CatOffset = CategoryEndOffsets.find(
Priority);
370 assert(CatOffset != CategoryEndOffsets.end());
371 unsigned InsertOffset = CatOffset->second;
373 if (
Iter != IncludesByPriority.end()) {
374 for (
const auto *Inc :
Iter->second) {
375 if (QuotedName < Inc->Name) {
376 InsertOffset = Inc->R.getOffset();
381 assert(InsertOffset <= Code.size());
382 llvm::StringRef DirectiveSpelling =
384 std::string NewInclude =
385 llvm::formatv(
"#{0} {1}\n", DirectiveSpelling, QuotedName);
390 if (InsertOffset == Code.size() && (!Code.empty() && Code.back() !=
'\n'))
391 NewInclude =
"\n" + NewInclude;
396 bool IsAngled)
const {
397 assert(IncludeName == trimInclude(IncludeName));
399 auto Iter = ExistingIncludes.find(IncludeName);
400 if (
Iter == ExistingIncludes.end())
402 for (
const auto &Inc :
Iter->second) {
403 if ((IsAngled && StringRef(Inc.Name).startswith(
"\"")) ||
404 (!IsAngled && StringRef(Inc.Name).startswith(
"<")))
407 FileName, Inc.R.getOffset(), Inc.R.getLength(),
""));
409 auto ErrMsg =
"Unexpected conflicts in #include deletions: " +
410 llvm::toString(std::move(Err));
411 llvm_unreachable(ErrMsg.c_str());
Defines the clang::FileManager interface and associated types.
Defines the SourceManager interface.
Directive - Abstract class representing a parsed verify directive.
SmallVector< BoundNodes, 1 > match(MatcherT Matcher, const NodeT &Node, ASTContext &Context)
Returns the results of matching Matcher on Node.
@ Result
The result type of a method or function.