10b57cec5SDimitry Andric //===--- HeaderIncludes.cpp - Insert/Delete #includes --*- C++ -*----------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "clang/Tooling/Inclusions/HeaderIncludes.h" 105ffd83dbSDimitry Andric #include "clang/Basic/FileManager.h" 110b57cec5SDimitry Andric #include "clang/Basic/SourceManager.h" 120b57cec5SDimitry Andric #include "clang/Lex/Lexer.h" 130b57cec5SDimitry Andric #include "llvm/Support/FormatVariadic.h" 14e8d8bef9SDimitry Andric #include "llvm/Support/Path.h" 15bdd1243dSDimitry Andric #include <optional> 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric namespace clang { 180b57cec5SDimitry Andric namespace tooling { 190b57cec5SDimitry Andric namespace { 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric LangOptions createLangOpts() { 220b57cec5SDimitry Andric LangOptions LangOpts; 230b57cec5SDimitry Andric LangOpts.CPlusPlus = 1; 240b57cec5SDimitry Andric LangOpts.CPlusPlus11 = 1; 250b57cec5SDimitry Andric LangOpts.CPlusPlus14 = 1; 260b57cec5SDimitry Andric LangOpts.LineComment = 1; 270b57cec5SDimitry Andric LangOpts.CXXOperatorNames = 1; 280b57cec5SDimitry Andric LangOpts.Bool = 1; 290b57cec5SDimitry Andric LangOpts.ObjC = 1; 300b57cec5SDimitry Andric LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally. 310b57cec5SDimitry Andric LangOpts.DeclSpecKeyword = 1; // To get __declspec. 320b57cec5SDimitry Andric LangOpts.WChar = 1; // To get wchar_t 330b57cec5SDimitry Andric return LangOpts; 340b57cec5SDimitry Andric } 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric // Returns the offset after skipping a sequence of tokens, matched by \p 370b57cec5SDimitry Andric // GetOffsetAfterSequence, from the start of the code. 380b57cec5SDimitry Andric // \p GetOffsetAfterSequence should be a function that matches a sequence of 390b57cec5SDimitry Andric // tokens and returns an offset after the sequence. 400b57cec5SDimitry Andric unsigned getOffsetAfterTokenSequence( 410b57cec5SDimitry Andric StringRef FileName, StringRef Code, const IncludeStyle &Style, 420b57cec5SDimitry Andric llvm::function_ref<unsigned(const SourceManager &, Lexer &, Token &)> 430b57cec5SDimitry Andric GetOffsetAfterSequence) { 440b57cec5SDimitry Andric SourceManagerForFile VirtualSM(FileName, Code); 450b57cec5SDimitry Andric SourceManager &SM = VirtualSM.get(); 4681ad6265SDimitry Andric LangOptions LangOpts = createLangOpts(); 47e8d8bef9SDimitry Andric Lexer Lex(SM.getMainFileID(), SM.getBufferOrFake(SM.getMainFileID()), SM, 4881ad6265SDimitry Andric LangOpts); 490b57cec5SDimitry Andric Token Tok; 500b57cec5SDimitry Andric // Get the first token. 510b57cec5SDimitry Andric Lex.LexFromRawLexer(Tok); 520b57cec5SDimitry Andric return GetOffsetAfterSequence(SM, Lex, Tok); 530b57cec5SDimitry Andric } 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric // Check if a sequence of tokens is like "#<Name> <raw_identifier>". If it is, 560b57cec5SDimitry Andric // \p Tok will be the token after this directive; otherwise, it can be any token 570b57cec5SDimitry Andric // after the given \p Tok (including \p Tok). If \p RawIDName is provided, the 580b57cec5SDimitry Andric // (second) raw_identifier name is checked. 590b57cec5SDimitry Andric bool checkAndConsumeDirectiveWithName( 600b57cec5SDimitry Andric Lexer &Lex, StringRef Name, Token &Tok, 61bdd1243dSDimitry Andric std::optional<StringRef> RawIDName = std::nullopt) { 620b57cec5SDimitry Andric bool Matched = Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) && 630b57cec5SDimitry Andric Tok.is(tok::raw_identifier) && 640b57cec5SDimitry Andric Tok.getRawIdentifier() == Name && !Lex.LexFromRawLexer(Tok) && 650b57cec5SDimitry Andric Tok.is(tok::raw_identifier) && 660b57cec5SDimitry Andric (!RawIDName || Tok.getRawIdentifier() == *RawIDName); 670b57cec5SDimitry Andric if (Matched) 680b57cec5SDimitry Andric Lex.LexFromRawLexer(Tok); 690b57cec5SDimitry Andric return Matched; 700b57cec5SDimitry Andric } 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric void skipComments(Lexer &Lex, Token &Tok) { 730b57cec5SDimitry Andric while (Tok.is(tok::comment)) 740b57cec5SDimitry Andric if (Lex.LexFromRawLexer(Tok)) 750b57cec5SDimitry Andric return; 760b57cec5SDimitry Andric } 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric // Returns the offset after header guard directives and any comments 790b57cec5SDimitry Andric // before/after header guards (e.g. #ifndef/#define pair, #pragma once). If no 800b57cec5SDimitry Andric // header guard is present in the code, this will return the offset after 810b57cec5SDimitry Andric // skipping all comments from the start of the code. 820b57cec5SDimitry Andric unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName, 830b57cec5SDimitry Andric StringRef Code, 840b57cec5SDimitry Andric const IncludeStyle &Style) { 850b57cec5SDimitry Andric // \p Consume returns location after header guard or 0 if no header guard is 860b57cec5SDimitry Andric // found. 870b57cec5SDimitry Andric auto ConsumeHeaderGuardAndComment = 880b57cec5SDimitry Andric [&](std::function<unsigned(const SourceManager &SM, Lexer &Lex, 890b57cec5SDimitry Andric Token Tok)> 900b57cec5SDimitry Andric Consume) { 910b57cec5SDimitry Andric return getOffsetAfterTokenSequence( 920b57cec5SDimitry Andric FileName, Code, Style, 930b57cec5SDimitry Andric [&Consume](const SourceManager &SM, Lexer &Lex, Token Tok) { 940b57cec5SDimitry Andric skipComments(Lex, Tok); 950b57cec5SDimitry Andric unsigned InitialOffset = SM.getFileOffset(Tok.getLocation()); 960b57cec5SDimitry Andric return std::max(InitialOffset, Consume(SM, Lex, Tok)); 970b57cec5SDimitry Andric }); 980b57cec5SDimitry Andric }; 990b57cec5SDimitry Andric return std::max( 1000b57cec5SDimitry Andric // #ifndef/#define 1010b57cec5SDimitry Andric ConsumeHeaderGuardAndComment( 1020b57cec5SDimitry Andric [](const SourceManager &SM, Lexer &Lex, Token Tok) -> unsigned { 1030b57cec5SDimitry Andric if (checkAndConsumeDirectiveWithName(Lex, "ifndef", Tok)) { 1040b57cec5SDimitry Andric skipComments(Lex, Tok); 105e8d8bef9SDimitry Andric if (checkAndConsumeDirectiveWithName(Lex, "define", Tok) && 106e8d8bef9SDimitry Andric Tok.isAtStartOfLine()) 1070b57cec5SDimitry Andric return SM.getFileOffset(Tok.getLocation()); 1080b57cec5SDimitry Andric } 1090b57cec5SDimitry Andric return 0; 1100b57cec5SDimitry Andric }), 1110b57cec5SDimitry Andric // #pragma once 1120b57cec5SDimitry Andric ConsumeHeaderGuardAndComment( 1130b57cec5SDimitry Andric [](const SourceManager &SM, Lexer &Lex, Token Tok) -> unsigned { 1140b57cec5SDimitry Andric if (checkAndConsumeDirectiveWithName(Lex, "pragma", Tok, 1150b57cec5SDimitry Andric StringRef("once"))) 1160b57cec5SDimitry Andric return SM.getFileOffset(Tok.getLocation()); 1170b57cec5SDimitry Andric return 0; 1180b57cec5SDimitry Andric })); 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric // Check if a sequence of tokens is like 1220b57cec5SDimitry Andric // "#include ("header.h" | <header.h>)". 1230b57cec5SDimitry Andric // If it is, \p Tok will be the token after this directive; otherwise, it can be 1240b57cec5SDimitry Andric // any token after the given \p Tok (including \p Tok). 1250b57cec5SDimitry Andric bool checkAndConsumeInclusiveDirective(Lexer &Lex, Token &Tok) { 1260b57cec5SDimitry Andric auto Matched = [&]() { 1270b57cec5SDimitry Andric Lex.LexFromRawLexer(Tok); 1280b57cec5SDimitry Andric return true; 1290b57cec5SDimitry Andric }; 1300b57cec5SDimitry Andric if (Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) && 1310b57cec5SDimitry Andric Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "include") { 1320b57cec5SDimitry Andric if (Lex.LexFromRawLexer(Tok)) 1330b57cec5SDimitry Andric return false; 1340b57cec5SDimitry Andric if (Tok.is(tok::string_literal)) 1350b57cec5SDimitry Andric return Matched(); 1360b57cec5SDimitry Andric if (Tok.is(tok::less)) { 1370b57cec5SDimitry Andric while (!Lex.LexFromRawLexer(Tok) && Tok.isNot(tok::greater)) { 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric if (Tok.is(tok::greater)) 1400b57cec5SDimitry Andric return Matched(); 1410b57cec5SDimitry Andric } 1420b57cec5SDimitry Andric } 1430b57cec5SDimitry Andric return false; 1440b57cec5SDimitry Andric } 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric // Returns the offset of the last #include directive after which a new 1470b57cec5SDimitry Andric // #include can be inserted. This ignores #include's after the #include block(s) 1480b57cec5SDimitry Andric // in the beginning of a file to avoid inserting headers into code sections 1490b57cec5SDimitry Andric // where new #include's should not be added by default. 1500b57cec5SDimitry Andric // These code sections include: 1510b57cec5SDimitry Andric // - raw string literals (containing #include). 1520b57cec5SDimitry Andric // - #if blocks. 1530b57cec5SDimitry Andric // - Special #include's among declarations (e.g. functions). 1540b57cec5SDimitry Andric // 1550b57cec5SDimitry Andric // If no #include after which a new #include can be inserted, this returns the 1560b57cec5SDimitry Andric // offset after skipping all comments from the start of the code. 1570b57cec5SDimitry Andric // Inserting after an #include is not allowed if it comes after code that is not 1580b57cec5SDimitry Andric // #include (e.g. pre-processing directive that is not #include, declarations). 1590b57cec5SDimitry Andric unsigned getMaxHeaderInsertionOffset(StringRef FileName, StringRef Code, 1600b57cec5SDimitry Andric const IncludeStyle &Style) { 1610b57cec5SDimitry Andric return getOffsetAfterTokenSequence( 1620b57cec5SDimitry Andric FileName, Code, Style, 1630b57cec5SDimitry Andric [](const SourceManager &SM, Lexer &Lex, Token Tok) { 1640b57cec5SDimitry Andric skipComments(Lex, Tok); 1650b57cec5SDimitry Andric unsigned MaxOffset = SM.getFileOffset(Tok.getLocation()); 1660b57cec5SDimitry Andric while (checkAndConsumeInclusiveDirective(Lex, Tok)) 1670b57cec5SDimitry Andric MaxOffset = SM.getFileOffset(Tok.getLocation()); 1680b57cec5SDimitry Andric return MaxOffset; 1690b57cec5SDimitry Andric }); 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric inline StringRef trimInclude(StringRef IncludeName) { 1730b57cec5SDimitry Andric return IncludeName.trim("\"<>"); 1740b57cec5SDimitry Andric } 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric const char IncludeRegexPattern[] = 1770b57cec5SDimitry Andric R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))"; 1780b57cec5SDimitry Andric 179e8d8bef9SDimitry Andric // The filename of Path excluding extension. 180e8d8bef9SDimitry Andric // Used to match implementation with headers, this differs from sys::path::stem: 181e8d8bef9SDimitry Andric // - in names with multiple dots (foo.cu.cc) it terminates at the *first* 182e8d8bef9SDimitry Andric // - an empty stem is never returned: /foo/.bar.x => .bar 183e8d8bef9SDimitry Andric // - we don't bother to handle . and .. specially 184e8d8bef9SDimitry Andric StringRef matchingStem(llvm::StringRef Path) { 185e8d8bef9SDimitry Andric StringRef Name = llvm::sys::path::filename(Path); 186e8d8bef9SDimitry Andric return Name.substr(0, Name.find('.', 1)); 187e8d8bef9SDimitry Andric } 188e8d8bef9SDimitry Andric 1890b57cec5SDimitry Andric } // anonymous namespace 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric IncludeCategoryManager::IncludeCategoryManager(const IncludeStyle &Style, 1920b57cec5SDimitry Andric StringRef FileName) 1930b57cec5SDimitry Andric : Style(Style), FileName(FileName) { 194e8d8bef9SDimitry Andric for (const auto &Category : Style.IncludeCategories) { 195e8d8bef9SDimitry Andric CategoryRegexs.emplace_back(Category.Regex, Category.RegexIsCaseSensitive 196e8d8bef9SDimitry Andric ? llvm::Regex::NoFlags 197e8d8bef9SDimitry Andric : llvm::Regex::IgnoreCase); 198e8d8bef9SDimitry Andric } 1995f757f3fSDimitry Andric IsMainFile = FileName.ends_with(".c") || FileName.ends_with(".cc") || 2005f757f3fSDimitry Andric FileName.ends_with(".cpp") || FileName.ends_with(".c++") || 2015f757f3fSDimitry Andric FileName.ends_with(".cxx") || FileName.ends_with(".m") || 2025f757f3fSDimitry Andric FileName.ends_with(".mm"); 203480093f4SDimitry Andric if (!Style.IncludeIsMainSourceRegex.empty()) { 204480093f4SDimitry Andric llvm::Regex MainFileRegex(Style.IncludeIsMainSourceRegex); 205480093f4SDimitry Andric IsMainFile |= MainFileRegex.match(FileName); 206480093f4SDimitry Andric } 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric int IncludeCategoryManager::getIncludePriority(StringRef IncludeName, 2100b57cec5SDimitry Andric bool CheckMainHeader) const { 2110b57cec5SDimitry Andric int Ret = INT_MAX; 2120b57cec5SDimitry Andric for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i) 2130b57cec5SDimitry Andric if (CategoryRegexs[i].match(IncludeName)) { 2140b57cec5SDimitry Andric Ret = Style.IncludeCategories[i].Priority; 2150b57cec5SDimitry Andric break; 2160b57cec5SDimitry Andric } 2170b57cec5SDimitry Andric if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName)) 2180b57cec5SDimitry Andric Ret = 0; 2190b57cec5SDimitry Andric return Ret; 2200b57cec5SDimitry Andric } 2210b57cec5SDimitry Andric 222a7dea167SDimitry Andric int IncludeCategoryManager::getSortIncludePriority(StringRef IncludeName, 223a7dea167SDimitry Andric bool CheckMainHeader) const { 224a7dea167SDimitry Andric int Ret = INT_MAX; 225a7dea167SDimitry Andric for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i) 226a7dea167SDimitry Andric if (CategoryRegexs[i].match(IncludeName)) { 227a7dea167SDimitry Andric Ret = Style.IncludeCategories[i].SortPriority; 228a7dea167SDimitry Andric if (Ret == 0) 229a7dea167SDimitry Andric Ret = Style.IncludeCategories[i].Priority; 230a7dea167SDimitry Andric break; 231a7dea167SDimitry Andric } 232a7dea167SDimitry Andric if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName)) 233a7dea167SDimitry Andric Ret = 0; 234a7dea167SDimitry Andric return Ret; 235a7dea167SDimitry Andric } 2360b57cec5SDimitry Andric bool IncludeCategoryManager::isMainHeader(StringRef IncludeName) const { 237*0fca6ea1SDimitry Andric switch (Style.MainIncludeChar) { 238*0fca6ea1SDimitry Andric case IncludeStyle::MICD_Quote: 2395f757f3fSDimitry Andric if (!IncludeName.starts_with("\"")) 2400b57cec5SDimitry Andric return false; 241*0fca6ea1SDimitry Andric break; 242*0fca6ea1SDimitry Andric case IncludeStyle::MICD_AngleBracket: 243*0fca6ea1SDimitry Andric if (!IncludeName.starts_with("<")) 244*0fca6ea1SDimitry Andric return false; 245*0fca6ea1SDimitry Andric break; 246*0fca6ea1SDimitry Andric case IncludeStyle::MICD_Any: 247*0fca6ea1SDimitry Andric break; 248*0fca6ea1SDimitry Andric } 249e8d8bef9SDimitry Andric 250e8d8bef9SDimitry Andric IncludeName = 251e8d8bef9SDimitry Andric IncludeName.drop_front(1).drop_back(1); // remove the surrounding "" or <> 252e8d8bef9SDimitry Andric // Not matchingStem: implementation files may have compound extensions but 253e8d8bef9SDimitry Andric // headers may not. 254e8d8bef9SDimitry Andric StringRef HeaderStem = llvm::sys::path::stem(IncludeName); 255e8d8bef9SDimitry Andric StringRef FileStem = llvm::sys::path::stem(FileName); // foo.cu for foo.cu.cc 256e8d8bef9SDimitry Andric StringRef MatchingFileStem = matchingStem(FileName); // foo for foo.cu.cc 257e8d8bef9SDimitry Andric // main-header examples: 258e8d8bef9SDimitry Andric // 1) foo.h => foo.cc 259e8d8bef9SDimitry Andric // 2) foo.h => foo.cu.cc 260e8d8bef9SDimitry Andric // 3) foo.proto.h => foo.proto.cc 261e8d8bef9SDimitry Andric // 262e8d8bef9SDimitry Andric // non-main-header examples: 263e8d8bef9SDimitry Andric // 1) foo.h => bar.cc 264e8d8bef9SDimitry Andric // 2) foo.proto.h => foo.cc 265e8d8bef9SDimitry Andric StringRef Matching; 26606c3fb27SDimitry Andric if (MatchingFileStem.starts_with_insensitive(HeaderStem)) 267e8d8bef9SDimitry Andric Matching = MatchingFileStem; // example 1), 2) 268fe6060f1SDimitry Andric else if (FileStem.equals_insensitive(HeaderStem)) 269e8d8bef9SDimitry Andric Matching = FileStem; // example 3) 270e8d8bef9SDimitry Andric if (!Matching.empty()) { 2710b57cec5SDimitry Andric llvm::Regex MainIncludeRegex(HeaderStem.str() + Style.IncludeIsMainRegex, 2720b57cec5SDimitry Andric llvm::Regex::IgnoreCase); 273e8d8bef9SDimitry Andric if (MainIncludeRegex.match(Matching)) 2740b57cec5SDimitry Andric return true; 2750b57cec5SDimitry Andric } 2760b57cec5SDimitry Andric return false; 2770b57cec5SDimitry Andric } 2780b57cec5SDimitry Andric 279bdd1243dSDimitry Andric const llvm::Regex HeaderIncludes::IncludeRegex(IncludeRegexPattern); 280bdd1243dSDimitry Andric 2810b57cec5SDimitry Andric HeaderIncludes::HeaderIncludes(StringRef FileName, StringRef Code, 2820b57cec5SDimitry Andric const IncludeStyle &Style) 2830b57cec5SDimitry Andric : FileName(FileName), Code(Code), FirstIncludeOffset(-1), 2840b57cec5SDimitry Andric MinInsertOffset( 2850b57cec5SDimitry Andric getOffsetAfterHeaderGuardsAndComments(FileName, Code, Style)), 2860b57cec5SDimitry Andric MaxInsertOffset(MinInsertOffset + 2870b57cec5SDimitry Andric getMaxHeaderInsertionOffset( 2880b57cec5SDimitry Andric FileName, Code.drop_front(MinInsertOffset), Style)), 28906c3fb27SDimitry Andric MainIncludeFound(false), 290bdd1243dSDimitry Andric Categories(Style, FileName) { 2910b57cec5SDimitry Andric // Add 0 for main header and INT_MAX for headers that are not in any 2920b57cec5SDimitry Andric // category. 2930b57cec5SDimitry Andric Priorities = {0, INT_MAX}; 2940b57cec5SDimitry Andric for (const auto &Category : Style.IncludeCategories) 2950b57cec5SDimitry Andric Priorities.insert(Category.Priority); 2960b57cec5SDimitry Andric SmallVector<StringRef, 32> Lines; 2970b57cec5SDimitry Andric Code.drop_front(MinInsertOffset).split(Lines, "\n"); 2980b57cec5SDimitry Andric 2990b57cec5SDimitry Andric unsigned Offset = MinInsertOffset; 3000b57cec5SDimitry Andric unsigned NextLineOffset; 3010b57cec5SDimitry Andric SmallVector<StringRef, 4> Matches; 3020b57cec5SDimitry Andric for (auto Line : Lines) { 3030b57cec5SDimitry Andric NextLineOffset = std::min(Code.size(), Offset + Line.size() + 1); 3040b57cec5SDimitry Andric if (IncludeRegex.match(Line, &Matches)) { 3050b57cec5SDimitry Andric // If this is the last line without trailing newline, we need to make 3060b57cec5SDimitry Andric // sure we don't delete across the file boundary. 3070b57cec5SDimitry Andric addExistingInclude( 3080b57cec5SDimitry Andric Include(Matches[2], 3090b57cec5SDimitry Andric tooling::Range( 310bdd1243dSDimitry Andric Offset, std::min(Line.size() + 1, Code.size() - Offset)), 311bdd1243dSDimitry Andric Matches[1] == "import" ? tooling::IncludeDirective::Import 312bdd1243dSDimitry Andric : tooling::IncludeDirective::Include), 3130b57cec5SDimitry Andric NextLineOffset); 3140b57cec5SDimitry Andric } 3150b57cec5SDimitry Andric Offset = NextLineOffset; 3160b57cec5SDimitry Andric } 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric // Populate CategoryEndOfssets: 3190b57cec5SDimitry Andric // - Ensure that CategoryEndOffset[Highest] is always populated. 3200b57cec5SDimitry Andric // - If CategoryEndOffset[Priority] isn't set, use the next higher value 3210b57cec5SDimitry Andric // that is set, up to CategoryEndOffset[Highest]. 3220b57cec5SDimitry Andric auto Highest = Priorities.begin(); 3230b57cec5SDimitry Andric if (CategoryEndOffsets.find(*Highest) == CategoryEndOffsets.end()) { 3240b57cec5SDimitry Andric if (FirstIncludeOffset >= 0) 3250b57cec5SDimitry Andric CategoryEndOffsets[*Highest] = FirstIncludeOffset; 3260b57cec5SDimitry Andric else 3270b57cec5SDimitry Andric CategoryEndOffsets[*Highest] = MinInsertOffset; 3280b57cec5SDimitry Andric } 3290b57cec5SDimitry Andric // By this point, CategoryEndOffset[Highest] is always set appropriately: 3300b57cec5SDimitry Andric // - to an appropriate location before/after existing #includes, or 3310b57cec5SDimitry Andric // - to right after the header guard, or 3320b57cec5SDimitry Andric // - to the beginning of the file. 3330b57cec5SDimitry Andric for (auto I = ++Priorities.begin(), E = Priorities.end(); I != E; ++I) 3340b57cec5SDimitry Andric if (CategoryEndOffsets.find(*I) == CategoryEndOffsets.end()) 3350b57cec5SDimitry Andric CategoryEndOffsets[*I] = CategoryEndOffsets[*std::prev(I)]; 3360b57cec5SDimitry Andric } 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric // \p Offset: the start of the line following this include directive. 3390b57cec5SDimitry Andric void HeaderIncludes::addExistingInclude(Include IncludeToAdd, 3400b57cec5SDimitry Andric unsigned NextLineOffset) { 3410b57cec5SDimitry Andric auto Iter = 3420b57cec5SDimitry Andric ExistingIncludes.try_emplace(trimInclude(IncludeToAdd.Name)).first; 3430b57cec5SDimitry Andric Iter->second.push_back(std::move(IncludeToAdd)); 3440b57cec5SDimitry Andric auto &CurInclude = Iter->second.back(); 3450b57cec5SDimitry Andric // The header name with quotes or angle brackets. 3460b57cec5SDimitry Andric // Only record the offset of current #include if we can insert after it. 3470b57cec5SDimitry Andric if (CurInclude.R.getOffset() <= MaxInsertOffset) { 3480b57cec5SDimitry Andric int Priority = Categories.getIncludePriority( 34906c3fb27SDimitry Andric CurInclude.Name, /*CheckMainHeader=*/!MainIncludeFound); 35006c3fb27SDimitry Andric if (Priority == 0) 35106c3fb27SDimitry Andric MainIncludeFound = true; 3520b57cec5SDimitry Andric CategoryEndOffsets[Priority] = NextLineOffset; 3530b57cec5SDimitry Andric IncludesByPriority[Priority].push_back(&CurInclude); 3540b57cec5SDimitry Andric if (FirstIncludeOffset < 0) 3550b57cec5SDimitry Andric FirstIncludeOffset = CurInclude.R.getOffset(); 3560b57cec5SDimitry Andric } 3570b57cec5SDimitry Andric } 3580b57cec5SDimitry Andric 359bdd1243dSDimitry Andric std::optional<tooling::Replacement> 360bdd1243dSDimitry Andric HeaderIncludes::insert(llvm::StringRef IncludeName, bool IsAngled, 361bdd1243dSDimitry Andric IncludeDirective Directive) const { 3620b57cec5SDimitry Andric assert(IncludeName == trimInclude(IncludeName)); 3630b57cec5SDimitry Andric // If a <header> ("header") already exists in code, "header" (<header>) with 364bdd1243dSDimitry Andric // different quotation and/or directive will still be inserted. 3650b57cec5SDimitry Andric // FIXME: figure out if this is the best behavior. 3660b57cec5SDimitry Andric auto It = ExistingIncludes.find(IncludeName); 367bdd1243dSDimitry Andric if (It != ExistingIncludes.end()) { 3680b57cec5SDimitry Andric for (const auto &Inc : It->second) 369bdd1243dSDimitry Andric if (Inc.Directive == Directive && 3705f757f3fSDimitry Andric ((IsAngled && StringRef(Inc.Name).starts_with("<")) || 3715f757f3fSDimitry Andric (!IsAngled && StringRef(Inc.Name).starts_with("\"")))) 372bdd1243dSDimitry Andric return std::nullopt; 373bdd1243dSDimitry Andric } 3740b57cec5SDimitry Andric std::string Quoted = 3755ffd83dbSDimitry Andric std::string(llvm::formatv(IsAngled ? "<{0}>" : "\"{0}\"", IncludeName)); 3760b57cec5SDimitry Andric StringRef QuotedName = Quoted; 3770b57cec5SDimitry Andric int Priority = Categories.getIncludePriority( 37806c3fb27SDimitry Andric QuotedName, /*CheckMainHeader=*/!MainIncludeFound); 3790b57cec5SDimitry Andric auto CatOffset = CategoryEndOffsets.find(Priority); 3800b57cec5SDimitry Andric assert(CatOffset != CategoryEndOffsets.end()); 3810b57cec5SDimitry Andric unsigned InsertOffset = CatOffset->second; // Fall back offset 3820b57cec5SDimitry Andric auto Iter = IncludesByPriority.find(Priority); 3830b57cec5SDimitry Andric if (Iter != IncludesByPriority.end()) { 3840b57cec5SDimitry Andric for (const auto *Inc : Iter->second) { 3850b57cec5SDimitry Andric if (QuotedName < Inc->Name) { 3860b57cec5SDimitry Andric InsertOffset = Inc->R.getOffset(); 3870b57cec5SDimitry Andric break; 3880b57cec5SDimitry Andric } 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric } 3910b57cec5SDimitry Andric assert(InsertOffset <= Code.size()); 392bdd1243dSDimitry Andric llvm::StringRef DirectiveSpelling = 393bdd1243dSDimitry Andric Directive == IncludeDirective::Include ? "include" : "import"; 3945ffd83dbSDimitry Andric std::string NewInclude = 395bdd1243dSDimitry Andric llvm::formatv("#{0} {1}\n", DirectiveSpelling, QuotedName); 3960b57cec5SDimitry Andric // When inserting headers at end of the code, also append '\n' to the code 3970b57cec5SDimitry Andric // if it does not end with '\n'. 3980b57cec5SDimitry Andric // FIXME: when inserting multiple #includes at the end of code, only one 3990b57cec5SDimitry Andric // newline should be added. 4000b57cec5SDimitry Andric if (InsertOffset == Code.size() && (!Code.empty() && Code.back() != '\n')) 4010b57cec5SDimitry Andric NewInclude = "\n" + NewInclude; 4020b57cec5SDimitry Andric return tooling::Replacement(FileName, InsertOffset, 0, NewInclude); 4030b57cec5SDimitry Andric } 4040b57cec5SDimitry Andric 4050b57cec5SDimitry Andric tooling::Replacements HeaderIncludes::remove(llvm::StringRef IncludeName, 4060b57cec5SDimitry Andric bool IsAngled) const { 4070b57cec5SDimitry Andric assert(IncludeName == trimInclude(IncludeName)); 4080b57cec5SDimitry Andric tooling::Replacements Result; 4090b57cec5SDimitry Andric auto Iter = ExistingIncludes.find(IncludeName); 4100b57cec5SDimitry Andric if (Iter == ExistingIncludes.end()) 4110b57cec5SDimitry Andric return Result; 4120b57cec5SDimitry Andric for (const auto &Inc : Iter->second) { 4135f757f3fSDimitry Andric if ((IsAngled && StringRef(Inc.Name).starts_with("\"")) || 4145f757f3fSDimitry Andric (!IsAngled && StringRef(Inc.Name).starts_with("<"))) 4150b57cec5SDimitry Andric continue; 4160b57cec5SDimitry Andric llvm::Error Err = Result.add(tooling::Replacement( 4170b57cec5SDimitry Andric FileName, Inc.R.getOffset(), Inc.R.getLength(), "")); 4180b57cec5SDimitry Andric if (Err) { 4190b57cec5SDimitry Andric auto ErrMsg = "Unexpected conflicts in #include deletions: " + 4200b57cec5SDimitry Andric llvm::toString(std::move(Err)); 4210b57cec5SDimitry Andric llvm_unreachable(ErrMsg.c_str()); 4220b57cec5SDimitry Andric } 4230b57cec5SDimitry Andric } 4240b57cec5SDimitry Andric return Result; 4250b57cec5SDimitry Andric } 4260b57cec5SDimitry Andric 4270b57cec5SDimitry Andric } // namespace tooling 4280b57cec5SDimitry Andric } // namespace clang 429