144564ac7SEric Liu //===--- HeaderIncludes.cpp - Insert/Delete #includes --*- C++ -*----------===//
244564ac7SEric Liu //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
644564ac7SEric Liu //
744564ac7SEric Liu //===----------------------------------------------------------------------===//
844564ac7SEric Liu 
944564ac7SEric Liu #include "clang/Tooling/Inclusions/HeaderIncludes.h"
10e08464fbSReid Kleckner #include "clang/Basic/FileManager.h"
1144564ac7SEric Liu #include "clang/Basic/SourceManager.h"
1244564ac7SEric Liu #include "clang/Lex/Lexer.h"
1324739613SEric Liu #include "llvm/ADT/Optional.h"
14bb898704SEric Liu #include "llvm/Support/FormatVariadic.h"
1526682562SSam McCall #include "llvm/Support/Path.h"
1644564ac7SEric Liu 
1744564ac7SEric Liu namespace clang {
1844564ac7SEric Liu namespace tooling {
1944564ac7SEric Liu namespace {
2044564ac7SEric Liu 
createLangOpts()2144564ac7SEric Liu LangOptions createLangOpts() {
2244564ac7SEric Liu   LangOptions LangOpts;
2344564ac7SEric Liu   LangOpts.CPlusPlus = 1;
2444564ac7SEric Liu   LangOpts.CPlusPlus11 = 1;
2544564ac7SEric Liu   LangOpts.CPlusPlus14 = 1;
2644564ac7SEric Liu   LangOpts.LineComment = 1;
2744564ac7SEric Liu   LangOpts.CXXOperatorNames = 1;
2844564ac7SEric Liu   LangOpts.Bool = 1;
29fa98390bSErik Pilkington   LangOpts.ObjC = 1;
3044564ac7SEric Liu   LangOpts.MicrosoftExt = 1;    // To get kw___try, kw___finally.
3144564ac7SEric Liu   LangOpts.DeclSpecKeyword = 1; // To get __declspec.
3244564ac7SEric Liu   LangOpts.WChar = 1;           // To get wchar_t
3344564ac7SEric Liu   return LangOpts;
3444564ac7SEric Liu }
3544564ac7SEric Liu 
3644564ac7SEric Liu // Returns the offset after skipping a sequence of tokens, matched by \p
3744564ac7SEric Liu // GetOffsetAfterSequence, from the start of the code.
3844564ac7SEric Liu // \p GetOffsetAfterSequence should be a function that matches a sequence of
3944564ac7SEric Liu // tokens and returns an offset after the sequence.
getOffsetAfterTokenSequence(StringRef FileName,StringRef Code,const IncludeStyle & Style,llvm::function_ref<unsigned (const SourceManager &,Lexer &,Token &)> GetOffsetAfterSequence)4044564ac7SEric Liu unsigned getOffsetAfterTokenSequence(
4144564ac7SEric Liu     StringRef FileName, StringRef Code, const IncludeStyle &Style,
4244564ac7SEric Liu     llvm::function_ref<unsigned(const SourceManager &, Lexer &, Token &)>
4344564ac7SEric Liu         GetOffsetAfterSequence) {
4444564ac7SEric Liu   SourceManagerForFile VirtualSM(FileName, Code);
4544564ac7SEric Liu   SourceManager &SM = VirtualSM.get();
46b3e2dac2SDawid Jurczak   LangOptions LangOpts = createLangOpts();
47b3eff6b7SDuncan P. N. Exon Smith   Lexer Lex(SM.getMainFileID(), SM.getBufferOrFake(SM.getMainFileID()), SM,
48b3e2dac2SDawid Jurczak             LangOpts);
4944564ac7SEric Liu   Token Tok;
5044564ac7SEric Liu   // Get the first token.
5144564ac7SEric Liu   Lex.LexFromRawLexer(Tok);
5244564ac7SEric Liu   return GetOffsetAfterSequence(SM, Lex, Tok);
5344564ac7SEric Liu }
5444564ac7SEric Liu 
5544564ac7SEric Liu // Check if a sequence of tokens is like "#<Name> <raw_identifier>". If it is,
5644564ac7SEric Liu // \p Tok will be the token after this directive; otherwise, it can be any token
5724739613SEric Liu // after the given \p Tok (including \p Tok). If \p RawIDName is provided, the
5824739613SEric Liu // (second) raw_identifier name is checked.
checkAndConsumeDirectiveWithName(Lexer & Lex,StringRef Name,Token & Tok,llvm::Optional<StringRef> RawIDName=llvm::None)5924739613SEric Liu bool checkAndConsumeDirectiveWithName(
6024739613SEric Liu     Lexer &Lex, StringRef Name, Token &Tok,
6124739613SEric Liu     llvm::Optional<StringRef> RawIDName = llvm::None) {
6244564ac7SEric Liu   bool Matched = Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&
6344564ac7SEric Liu                  Tok.is(tok::raw_identifier) &&
6444564ac7SEric Liu                  Tok.getRawIdentifier() == Name && !Lex.LexFromRawLexer(Tok) &&
6524739613SEric Liu                  Tok.is(tok::raw_identifier) &&
6624739613SEric Liu                  (!RawIDName || Tok.getRawIdentifier() == *RawIDName);
6744564ac7SEric Liu   if (Matched)
6844564ac7SEric Liu     Lex.LexFromRawLexer(Tok);
6944564ac7SEric Liu   return Matched;
7044564ac7SEric Liu }
7144564ac7SEric Liu 
skipComments(Lexer & Lex,Token & Tok)7244564ac7SEric Liu void skipComments(Lexer &Lex, Token &Tok) {
7344564ac7SEric Liu   while (Tok.is(tok::comment))
7444564ac7SEric Liu     if (Lex.LexFromRawLexer(Tok))
7544564ac7SEric Liu       return;
7644564ac7SEric Liu }
7744564ac7SEric Liu 
7844564ac7SEric Liu // Returns the offset after header guard directives and any comments
7924739613SEric Liu // before/after header guards (e.g. #ifndef/#define pair, #pragma once). If no
8024739613SEric Liu // header guard is present in the code, this will return the offset after
8124739613SEric Liu // skipping all comments from the start of the code.
getOffsetAfterHeaderGuardsAndComments(StringRef FileName,StringRef Code,const IncludeStyle & Style)8244564ac7SEric Liu unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName,
8344564ac7SEric Liu                                                StringRef Code,
8444564ac7SEric Liu                                                const IncludeStyle &Style) {
8524739613SEric Liu   // \p Consume returns location after header guard or 0 if no header guard is
8624739613SEric Liu   // found.
8724739613SEric Liu   auto ConsumeHeaderGuardAndComment =
8824739613SEric Liu       [&](std::function<unsigned(const SourceManager &SM, Lexer &Lex,
8924739613SEric Liu                                  Token Tok)>
9024739613SEric Liu               Consume) {
9144564ac7SEric Liu         return getOffsetAfterTokenSequence(
9244564ac7SEric Liu             FileName, Code, Style,
9324739613SEric Liu             [&Consume](const SourceManager &SM, Lexer &Lex, Token Tok) {
9444564ac7SEric Liu               skipComments(Lex, Tok);
9544564ac7SEric Liu               unsigned InitialOffset = SM.getFileOffset(Tok.getLocation());
9624739613SEric Liu               return std::max(InitialOffset, Consume(SM, Lex, Tok));
9724739613SEric Liu             });
9824739613SEric Liu       };
9924739613SEric Liu   return std::max(
10024739613SEric Liu       // #ifndef/#define
10124739613SEric Liu       ConsumeHeaderGuardAndComment(
10224739613SEric Liu           [](const SourceManager &SM, Lexer &Lex, Token Tok) -> unsigned {
10344564ac7SEric Liu             if (checkAndConsumeDirectiveWithName(Lex, "ifndef", Tok)) {
10444564ac7SEric Liu               skipComments(Lex, Tok);
105ea8e71c3SKadir Cetinkaya               if (checkAndConsumeDirectiveWithName(Lex, "define", Tok) &&
106ea8e71c3SKadir Cetinkaya                   Tok.isAtStartOfLine())
10744564ac7SEric Liu                 return SM.getFileOffset(Tok.getLocation());
10844564ac7SEric Liu             }
10924739613SEric Liu             return 0;
11024739613SEric Liu           }),
11124739613SEric Liu       // #pragma once
11224739613SEric Liu       ConsumeHeaderGuardAndComment(
11324739613SEric Liu           [](const SourceManager &SM, Lexer &Lex, Token Tok) -> unsigned {
11424739613SEric Liu             if (checkAndConsumeDirectiveWithName(Lex, "pragma", Tok,
11524739613SEric Liu                                                  StringRef("once")))
11624739613SEric Liu               return SM.getFileOffset(Tok.getLocation());
11724739613SEric Liu             return 0;
11824739613SEric Liu           }));
11944564ac7SEric Liu }
12044564ac7SEric Liu 
12144564ac7SEric Liu // Check if a sequence of tokens is like
12244564ac7SEric Liu //    "#include ("header.h" | <header.h>)".
12344564ac7SEric Liu // If it is, \p Tok will be the token after this directive; otherwise, it can be
12444564ac7SEric Liu // any token after the given \p Tok (including \p Tok).
checkAndConsumeInclusiveDirective(Lexer & Lex,Token & Tok)12544564ac7SEric Liu bool checkAndConsumeInclusiveDirective(Lexer &Lex, Token &Tok) {
12644564ac7SEric Liu   auto Matched = [&]() {
12744564ac7SEric Liu     Lex.LexFromRawLexer(Tok);
12844564ac7SEric Liu     return true;
12944564ac7SEric Liu   };
13044564ac7SEric Liu   if (Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&
13144564ac7SEric Liu       Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "include") {
13244564ac7SEric Liu     if (Lex.LexFromRawLexer(Tok))
13344564ac7SEric Liu       return false;
13444564ac7SEric Liu     if (Tok.is(tok::string_literal))
13544564ac7SEric Liu       return Matched();
13644564ac7SEric Liu     if (Tok.is(tok::less)) {
13744564ac7SEric Liu       while (!Lex.LexFromRawLexer(Tok) && Tok.isNot(tok::greater)) {
13844564ac7SEric Liu       }
13944564ac7SEric Liu       if (Tok.is(tok::greater))
14044564ac7SEric Liu         return Matched();
14144564ac7SEric Liu     }
14244564ac7SEric Liu   }
14344564ac7SEric Liu   return false;
14444564ac7SEric Liu }
14544564ac7SEric Liu 
14644564ac7SEric Liu // Returns the offset of the last #include directive after which a new
14744564ac7SEric Liu // #include can be inserted. This ignores #include's after the #include block(s)
14844564ac7SEric Liu // in the beginning of a file to avoid inserting headers into code sections
14944564ac7SEric Liu // where new #include's should not be added by default.
15044564ac7SEric Liu // These code sections include:
15144564ac7SEric Liu //      - raw string literals (containing #include).
15244564ac7SEric Liu //      - #if blocks.
15344564ac7SEric Liu //      - Special #include's among declarations (e.g. functions).
15444564ac7SEric Liu //
15544564ac7SEric Liu // If no #include after which a new #include can be inserted, this returns the
15644564ac7SEric Liu // offset after skipping all comments from the start of the code.
15744564ac7SEric Liu // Inserting after an #include is not allowed if it comes after code that is not
15844564ac7SEric Liu // #include (e.g. pre-processing directive that is not #include, declarations).
getMaxHeaderInsertionOffset(StringRef FileName,StringRef Code,const IncludeStyle & Style)15944564ac7SEric Liu unsigned getMaxHeaderInsertionOffset(StringRef FileName, StringRef Code,
16044564ac7SEric Liu                                      const IncludeStyle &Style) {
16144564ac7SEric Liu   return getOffsetAfterTokenSequence(
16244564ac7SEric Liu       FileName, Code, Style,
16344564ac7SEric Liu       [](const SourceManager &SM, Lexer &Lex, Token Tok) {
16444564ac7SEric Liu         skipComments(Lex, Tok);
16544564ac7SEric Liu         unsigned MaxOffset = SM.getFileOffset(Tok.getLocation());
16644564ac7SEric Liu         while (checkAndConsumeInclusiveDirective(Lex, Tok))
16744564ac7SEric Liu           MaxOffset = SM.getFileOffset(Tok.getLocation());
16844564ac7SEric Liu         return MaxOffset;
16944564ac7SEric Liu       });
17044564ac7SEric Liu }
17144564ac7SEric Liu 
trimInclude(StringRef IncludeName)172*e8cc7490SKrasimir Georgiev inline StringRef trimInclude(StringRef IncludeName) {
173*e8cc7490SKrasimir Georgiev   return IncludeName.trim("\"<>");
174*e8cc7490SKrasimir Georgiev }
175*e8cc7490SKrasimir Georgiev 
176*e8cc7490SKrasimir Georgiev const char IncludeRegexPattern[] =
177*e8cc7490SKrasimir Georgiev     R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))";
178*e8cc7490SKrasimir Georgiev 
17926682562SSam McCall // The filename of Path excluding extension.
18026682562SSam McCall // Used to match implementation with headers, this differs from sys::path::stem:
18126682562SSam McCall //  - in names with multiple dots (foo.cu.cc) it terminates at the *first*
18226682562SSam McCall //  - an empty stem is never returned: /foo/.bar.x => .bar
18326682562SSam McCall //  - we don't bother to handle . and .. specially
matchingStem(llvm::StringRef Path)18426682562SSam McCall StringRef matchingStem(llvm::StringRef Path) {
18526682562SSam McCall   StringRef Name = llvm::sys::path::filename(Path);
18626682562SSam McCall   return Name.substr(0, Name.find('.', 1));
18726682562SSam McCall }
18826682562SSam McCall 
18944564ac7SEric Liu } // anonymous namespace
19044564ac7SEric Liu 
IncludeCategoryManager(const IncludeStyle & Style,StringRef FileName)19144564ac7SEric Liu IncludeCategoryManager::IncludeCategoryManager(const IncludeStyle &Style,
19244564ac7SEric Liu                                                StringRef FileName)
19344564ac7SEric Liu     : Style(Style), FileName(FileName) {
1948668eae2Smydeveloperday   for (const auto &Category : Style.IncludeCategories) {
1958668eae2Smydeveloperday     CategoryRegexs.emplace_back(Category.Regex, Category.RegexIsCaseSensitive
1968668eae2Smydeveloperday                                                     ? llvm::Regex::NoFlags
1978668eae2Smydeveloperday                                                     : llvm::Regex::IgnoreCase);
1988668eae2Smydeveloperday   }
19944564ac7SEric Liu   IsMainFile = FileName.endswith(".c") || FileName.endswith(".cc") ||
20044564ac7SEric Liu                FileName.endswith(".cpp") || FileName.endswith(".c++") ||
20144564ac7SEric Liu                FileName.endswith(".cxx") || FileName.endswith(".m") ||
20244564ac7SEric Liu                FileName.endswith(".mm");
203335ac2ebSmydeveloperday   if (!Style.IncludeIsMainSourceRegex.empty()) {
204335ac2ebSmydeveloperday     llvm::Regex MainFileRegex(Style.IncludeIsMainSourceRegex);
205335ac2ebSmydeveloperday     IsMainFile |= MainFileRegex.match(FileName);
206335ac2ebSmydeveloperday   }
20744564ac7SEric Liu }
20844564ac7SEric Liu 
getIncludePriority(StringRef IncludeName,bool CheckMainHeader) const20944564ac7SEric Liu int IncludeCategoryManager::getIncludePriority(StringRef IncludeName,
21044564ac7SEric Liu                                                bool CheckMainHeader) const {
21144564ac7SEric Liu   int Ret = INT_MAX;
21244564ac7SEric Liu   for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i)
21344564ac7SEric Liu     if (CategoryRegexs[i].match(IncludeName)) {
21444564ac7SEric Liu       Ret = Style.IncludeCategories[i].Priority;
21544564ac7SEric Liu       break;
21644564ac7SEric Liu     }
21744564ac7SEric Liu   if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName))
21844564ac7SEric Liu     Ret = 0;
21944564ac7SEric Liu   return Ret;
22044564ac7SEric Liu }
22144564ac7SEric Liu 
getSortIncludePriority(StringRef IncludeName,bool CheckMainHeader) const22252e44b14SPaul Hoad int IncludeCategoryManager::getSortIncludePriority(StringRef IncludeName,
22352e44b14SPaul Hoad                                                    bool CheckMainHeader) const {
22452e44b14SPaul Hoad   int Ret = INT_MAX;
22552e44b14SPaul Hoad   for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i)
22652e44b14SPaul Hoad     if (CategoryRegexs[i].match(IncludeName)) {
22752e44b14SPaul Hoad       Ret = Style.IncludeCategories[i].SortPriority;
22852e44b14SPaul Hoad       if (Ret == 0)
22952e44b14SPaul Hoad         Ret = Style.IncludeCategories[i].Priority;
23052e44b14SPaul Hoad       break;
23152e44b14SPaul Hoad     }
23252e44b14SPaul Hoad   if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName))
23352e44b14SPaul Hoad     Ret = 0;
23452e44b14SPaul Hoad   return Ret;
23552e44b14SPaul Hoad }
isMainHeader(StringRef IncludeName) const23644564ac7SEric Liu bool IncludeCategoryManager::isMainHeader(StringRef IncludeName) const {
23744564ac7SEric Liu   if (!IncludeName.startswith("\""))
23844564ac7SEric Liu     return false;
239c1b209ccSHaojian Wu 
24084048e23SHaojian Wu   IncludeName =
24184048e23SHaojian Wu       IncludeName.drop_front(1).drop_back(1); // remove the surrounding "" or <>
242c1b209ccSHaojian Wu   // Not matchingStem: implementation files may have compound extensions but
243c1b209ccSHaojian Wu   // headers may not.
24484048e23SHaojian Wu   StringRef HeaderStem = llvm::sys::path::stem(IncludeName);
24584048e23SHaojian Wu   StringRef FileStem = llvm::sys::path::stem(FileName); // foo.cu for foo.cu.cc
24684048e23SHaojian Wu   StringRef MatchingFileStem = matchingStem(FileName);  // foo for foo.cu.cc
24784048e23SHaojian Wu   // main-header examples:
24884048e23SHaojian Wu   //  1) foo.h => foo.cc
24984048e23SHaojian Wu   //  2) foo.h => foo.cu.cc
25084048e23SHaojian Wu   //  3) foo.proto.h => foo.proto.cc
25184048e23SHaojian Wu   //
25284048e23SHaojian Wu   // non-main-header examples:
25384048e23SHaojian Wu   //  1) foo.h => bar.cc
25484048e23SHaojian Wu   //  2) foo.proto.h => foo.cc
25584048e23SHaojian Wu   StringRef Matching;
256e5c7c171SMartin Storsjö   if (MatchingFileStem.startswith_insensitive(HeaderStem))
25784048e23SHaojian Wu     Matching = MatchingFileStem; // example 1), 2)
258e5c7c171SMartin Storsjö   else if (FileStem.equals_insensitive(HeaderStem))
25984048e23SHaojian Wu     Matching = FileStem; // example 3)
26084048e23SHaojian Wu   if (!Matching.empty()) {
261bb898704SEric Liu     llvm::Regex MainIncludeRegex(HeaderStem.str() + Style.IncludeIsMainRegex,
26244564ac7SEric Liu                                  llvm::Regex::IgnoreCase);
26384048e23SHaojian Wu     if (MainIncludeRegex.match(Matching))
26444564ac7SEric Liu       return true;
26544564ac7SEric Liu   }
26644564ac7SEric Liu   return false;
26744564ac7SEric Liu }
26844564ac7SEric Liu 
HeaderIncludes(StringRef FileName,StringRef Code,const IncludeStyle & Style)26944564ac7SEric Liu HeaderIncludes::HeaderIncludes(StringRef FileName, StringRef Code,
27044564ac7SEric Liu                                const IncludeStyle &Style)
27144564ac7SEric Liu     : FileName(FileName), Code(Code), FirstIncludeOffset(-1),
27244564ac7SEric Liu       MinInsertOffset(
27344564ac7SEric Liu           getOffsetAfterHeaderGuardsAndComments(FileName, Code, Style)),
27444564ac7SEric Liu       MaxInsertOffset(MinInsertOffset +
27544564ac7SEric Liu                       getMaxHeaderInsertionOffset(
27644564ac7SEric Liu                           FileName, Code.drop_front(MinInsertOffset), Style)),
277*e8cc7490SKrasimir Georgiev       Categories(Style, FileName),
278*e8cc7490SKrasimir Georgiev       IncludeRegex(llvm::Regex(IncludeRegexPattern)) {
27944564ac7SEric Liu   // Add 0 for main header and INT_MAX for headers that are not in any
28044564ac7SEric Liu   // category.
28144564ac7SEric Liu   Priorities = {0, INT_MAX};
28244564ac7SEric Liu   for (const auto &Category : Style.IncludeCategories)
28344564ac7SEric Liu     Priorities.insert(Category.Priority);
28444564ac7SEric Liu   SmallVector<StringRef, 32> Lines;
28544564ac7SEric Liu   Code.drop_front(MinInsertOffset).split(Lines, "\n");
28644564ac7SEric Liu 
28744564ac7SEric Liu   unsigned Offset = MinInsertOffset;
28844564ac7SEric Liu   unsigned NextLineOffset;
28944564ac7SEric Liu   SmallVector<StringRef, 4> Matches;
29044564ac7SEric Liu   for (auto Line : Lines) {
29144564ac7SEric Liu     NextLineOffset = std::min(Code.size(), Offset + Line.size() + 1);
29244564ac7SEric Liu     if (IncludeRegex.match(Line, &Matches)) {
29344564ac7SEric Liu       // If this is the last line without trailing newline, we need to make
29444564ac7SEric Liu       // sure we don't delete across the file boundary.
29544564ac7SEric Liu       addExistingInclude(
296*e8cc7490SKrasimir Georgiev           Include(Matches[2],
29744564ac7SEric Liu                   tooling::Range(
29844564ac7SEric Liu                       Offset, std::min(Line.size() + 1, Code.size() - Offset))),
29944564ac7SEric Liu           NextLineOffset);
30044564ac7SEric Liu     }
30144564ac7SEric Liu     Offset = NextLineOffset;
30244564ac7SEric Liu   }
30344564ac7SEric Liu 
30444564ac7SEric Liu   // Populate CategoryEndOfssets:
30544564ac7SEric Liu   // - Ensure that CategoryEndOffset[Highest] is always populated.
30644564ac7SEric Liu   // - If CategoryEndOffset[Priority] isn't set, use the next higher value
30744564ac7SEric Liu   // that is set, up to CategoryEndOffset[Highest].
30844564ac7SEric Liu   auto Highest = Priorities.begin();
30944564ac7SEric Liu   if (CategoryEndOffsets.find(*Highest) == CategoryEndOffsets.end()) {
31044564ac7SEric Liu     if (FirstIncludeOffset >= 0)
31144564ac7SEric Liu       CategoryEndOffsets[*Highest] = FirstIncludeOffset;
31244564ac7SEric Liu     else
31344564ac7SEric Liu       CategoryEndOffsets[*Highest] = MinInsertOffset;
31444564ac7SEric Liu   }
31544564ac7SEric Liu   // By this point, CategoryEndOffset[Highest] is always set appropriately:
31644564ac7SEric Liu   //  - to an appropriate location before/after existing #includes, or
31744564ac7SEric Liu   //  - to right after the header guard, or
31844564ac7SEric Liu   //  - to the beginning of the file.
31944564ac7SEric Liu   for (auto I = ++Priorities.begin(), E = Priorities.end(); I != E; ++I)
32044564ac7SEric Liu     if (CategoryEndOffsets.find(*I) == CategoryEndOffsets.end())
32144564ac7SEric Liu       CategoryEndOffsets[*I] = CategoryEndOffsets[*std::prev(I)];
32244564ac7SEric Liu }
32344564ac7SEric Liu 
32444564ac7SEric Liu // \p Offset: the start of the line following this include directive.
addExistingInclude(Include IncludeToAdd,unsigned NextLineOffset)32544564ac7SEric Liu void HeaderIncludes::addExistingInclude(Include IncludeToAdd,
32644564ac7SEric Liu                                         unsigned NextLineOffset) {
32744564ac7SEric Liu   auto Iter =
32844564ac7SEric Liu       ExistingIncludes.try_emplace(trimInclude(IncludeToAdd.Name)).first;
32944564ac7SEric Liu   Iter->second.push_back(std::move(IncludeToAdd));
33044564ac7SEric Liu   auto &CurInclude = Iter->second.back();
33144564ac7SEric Liu   // The header name with quotes or angle brackets.
33244564ac7SEric Liu   // Only record the offset of current #include if we can insert after it.
33344564ac7SEric Liu   if (CurInclude.R.getOffset() <= MaxInsertOffset) {
33444564ac7SEric Liu     int Priority = Categories.getIncludePriority(
33544564ac7SEric Liu         CurInclude.Name, /*CheckMainHeader=*/FirstIncludeOffset < 0);
33644564ac7SEric Liu     CategoryEndOffsets[Priority] = NextLineOffset;
33744564ac7SEric Liu     IncludesByPriority[Priority].push_back(&CurInclude);
33844564ac7SEric Liu     if (FirstIncludeOffset < 0)
33944564ac7SEric Liu       FirstIncludeOffset = CurInclude.R.getOffset();
34044564ac7SEric Liu   }
34144564ac7SEric Liu }
34244564ac7SEric Liu 
34344564ac7SEric Liu llvm::Optional<tooling::Replacement>
insert(llvm::StringRef IncludeName,bool IsAngled) const34444564ac7SEric Liu HeaderIncludes::insert(llvm::StringRef IncludeName, bool IsAngled) const {
34544564ac7SEric Liu   assert(IncludeName == trimInclude(IncludeName));
34644564ac7SEric Liu   // If a <header> ("header") already exists in code, "header" (<header>) with
34744564ac7SEric Liu   // different quotation will still be inserted.
34844564ac7SEric Liu   // FIXME: figure out if this is the best behavior.
34944564ac7SEric Liu   auto It = ExistingIncludes.find(IncludeName);
35044564ac7SEric Liu   if (It != ExistingIncludes.end())
35144564ac7SEric Liu     for (const auto &Inc : It->second)
35244564ac7SEric Liu       if ((IsAngled && StringRef(Inc.Name).startswith("<")) ||
35344564ac7SEric Liu           (!IsAngled && StringRef(Inc.Name).startswith("\"")))
35444564ac7SEric Liu         return llvm::None;
355bb898704SEric Liu   std::string Quoted =
356adcd0268SBenjamin Kramer       std::string(llvm::formatv(IsAngled ? "<{0}>" : "\"{0}\"", IncludeName));
35744564ac7SEric Liu   StringRef QuotedName = Quoted;
35844564ac7SEric Liu   int Priority = Categories.getIncludePriority(
35944564ac7SEric Liu       QuotedName, /*CheckMainHeader=*/FirstIncludeOffset < 0);
36044564ac7SEric Liu   auto CatOffset = CategoryEndOffsets.find(Priority);
36144564ac7SEric Liu   assert(CatOffset != CategoryEndOffsets.end());
36244564ac7SEric Liu   unsigned InsertOffset = CatOffset->second; // Fall back offset
36344564ac7SEric Liu   auto Iter = IncludesByPriority.find(Priority);
36444564ac7SEric Liu   if (Iter != IncludesByPriority.end()) {
36544564ac7SEric Liu     for (const auto *Inc : Iter->second) {
36644564ac7SEric Liu       if (QuotedName < Inc->Name) {
36744564ac7SEric Liu         InsertOffset = Inc->R.getOffset();
36844564ac7SEric Liu         break;
36944564ac7SEric Liu       }
37044564ac7SEric Liu     }
37144564ac7SEric Liu   }
37244564ac7SEric Liu   assert(InsertOffset <= Code.size());
373adcd0268SBenjamin Kramer   std::string NewInclude =
374adcd0268SBenjamin Kramer       std::string(llvm::formatv("#include {0}\n", QuotedName));
37544564ac7SEric Liu   // When inserting headers at end of the code, also append '\n' to the code
37644564ac7SEric Liu   // if it does not end with '\n'.
37744564ac7SEric Liu   // FIXME: when inserting multiple #includes at the end of code, only one
37844564ac7SEric Liu   // newline should be added.
37944564ac7SEric Liu   if (InsertOffset == Code.size() && (!Code.empty() && Code.back() != '\n'))
38044564ac7SEric Liu     NewInclude = "\n" + NewInclude;
38144564ac7SEric Liu   return tooling::Replacement(FileName, InsertOffset, 0, NewInclude);
38244564ac7SEric Liu }
38344564ac7SEric Liu 
remove(llvm::StringRef IncludeName,bool IsAngled) const38444564ac7SEric Liu tooling::Replacements HeaderIncludes::remove(llvm::StringRef IncludeName,
38544564ac7SEric Liu                                              bool IsAngled) const {
38644564ac7SEric Liu   assert(IncludeName == trimInclude(IncludeName));
38744564ac7SEric Liu   tooling::Replacements Result;
38844564ac7SEric Liu   auto Iter = ExistingIncludes.find(IncludeName);
38944564ac7SEric Liu   if (Iter == ExistingIncludes.end())
39044564ac7SEric Liu     return Result;
39144564ac7SEric Liu   for (const auto &Inc : Iter->second) {
39244564ac7SEric Liu     if ((IsAngled && StringRef(Inc.Name).startswith("\"")) ||
39344564ac7SEric Liu         (!IsAngled && StringRef(Inc.Name).startswith("<")))
39444564ac7SEric Liu       continue;
39544564ac7SEric Liu     llvm::Error Err = Result.add(tooling::Replacement(
39644564ac7SEric Liu         FileName, Inc.R.getOffset(), Inc.R.getLength(), ""));
39744564ac7SEric Liu     if (Err) {
39844564ac7SEric Liu       auto ErrMsg = "Unexpected conflicts in #include deletions: " +
39944564ac7SEric Liu                     llvm::toString(std::move(Err));
40044564ac7SEric Liu       llvm_unreachable(ErrMsg.c_str());
40144564ac7SEric Liu     }
40244564ac7SEric Liu   }
40344564ac7SEric Liu   return Result;
40444564ac7SEric Liu }
40544564ac7SEric Liu 
40644564ac7SEric Liu } // namespace tooling
40744564ac7SEric Liu } // namespace clang
408