18 LangOptions createLangOpts() {
20 LangOpts.CPlusPlus = 1;
21 LangOpts.CPlusPlus11 = 1;
22 LangOpts.CPlusPlus14 = 1;
23 LangOpts.LineComment = 1;
24 LangOpts.CXXOperatorNames = 1;
28 LangOpts.MicrosoftExt = 1;
29 LangOpts.DeclSpecKeyword = 1;
38 unsigned getOffsetAfterTokenSequence(
39 StringRef FileName, StringRef Code,
const IncludeStyle &
Style,
40 llvm::function_ref<
unsigned(
const SourceManager &, Lexer &, Token &)>
41 GetOffsetAfterSequence) {
42 SourceManagerForFile VirtualSM(FileName, Code);
43 SourceManager &
SM = VirtualSM.get();
44 Lexer Lex(SM.getMainFileID(), SM.getBuffer(SM.getMainFileID()), SM,
48 Lex.LexFromRawLexer(Tok);
49 return GetOffsetAfterSequence(SM, Lex, Tok);
55 bool checkAndConsumeDirectiveWithName(Lexer &Lex, StringRef Name, Token &Tok) {
56 bool Matched = Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&
57 Tok.is(tok::raw_identifier) &&
58 Tok.getRawIdentifier() == Name && !Lex.LexFromRawLexer(Tok) &&
59 Tok.is(tok::raw_identifier);
61 Lex.LexFromRawLexer(Tok);
65 void skipComments(Lexer &Lex, Token &Tok) {
66 while (Tok.is(tok::comment))
67 if (Lex.LexFromRawLexer(Tok))
75 unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName,
77 const IncludeStyle &Style) {
78 return getOffsetAfterTokenSequence(
79 FileName, Code, Style,
80 [](
const SourceManager &SM, Lexer &Lex, Token Tok) {
81 skipComments(Lex, Tok);
82 unsigned InitialOffset = SM.getFileOffset(Tok.getLocation());
83 if (checkAndConsumeDirectiveWithName(Lex,
"ifndef", Tok)) {
84 skipComments(Lex, Tok);
85 if (checkAndConsumeDirectiveWithName(Lex,
"define", Tok))
86 return SM.getFileOffset(Tok.getLocation());
96 bool checkAndConsumeInclusiveDirective(Lexer &Lex, Token &Tok) {
97 auto Matched = [&]() {
98 Lex.LexFromRawLexer(Tok);
101 if (Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&
102 Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() ==
"include") {
103 if (Lex.LexFromRawLexer(Tok))
105 if (Tok.is(tok::string_literal))
107 if (Tok.is(tok::less)) {
108 while (!Lex.LexFromRawLexer(Tok) && Tok.isNot(tok::greater)) {
110 if (Tok.is(tok::greater))
130 unsigned getMaxHeaderInsertionOffset(StringRef FileName, StringRef Code,
131 const IncludeStyle &Style) {
132 return getOffsetAfterTokenSequence(
133 FileName, Code, Style,
134 [](
const SourceManager &SM, Lexer &Lex, Token Tok) {
135 skipComments(Lex, Tok);
136 unsigned MaxOffset = SM.getFileOffset(Tok.getLocation());
137 while (checkAndConsumeInclusiveDirective(Lex, Tok))
138 MaxOffset = SM.getFileOffset(Tok.getLocation());
143 inline StringRef trimInclude(StringRef IncludeName) {
144 return IncludeName.trim(
"\"<>");
147 const char IncludeRegexPattern[] =
148 R
"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))"; 154 : Style(Style), FileName(FileName) {
155 FileStem = llvm::sys::path::stem(FileName);
157 CategoryRegexs.emplace_back(
Category.Regex, llvm::Regex::IgnoreCase);
158 IsMainFile = FileName.endswith(
".c") || FileName.endswith(
".cc") ||
159 FileName.endswith(
".cpp") || FileName.endswith(
".c++") ||
160 FileName.endswith(
".cxx") || FileName.endswith(
".m") ||
161 FileName.endswith(
".mm");
165 bool CheckMainHeader)
const {
167 for (
unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i)
168 if (CategoryRegexs[i].
match(IncludeName)) {
169 Ret = Style.IncludeCategories[i].Priority;
172 if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName))
177 bool IncludeCategoryManager::isMainHeader(StringRef IncludeName)
const {
178 if (!IncludeName.startswith(
"\""))
180 StringRef HeaderStem =
181 llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1));
182 if (FileStem.startswith(HeaderStem) ||
183 FileStem.startswith_lower(HeaderStem)) {
184 llvm::Regex MainIncludeRegex((HeaderStem + Style.IncludeIsMainRegex).str(),
185 llvm::Regex::IgnoreCase);
186 if (MainIncludeRegex.match(FileStem))
194 : FileName(FileName), Code(Code), FirstIncludeOffset(-1),
196 getOffsetAfterHeaderGuardsAndComments(FileName, Code, Style)),
197 MaxInsertOffset(MinInsertOffset +
198 getMaxHeaderInsertionOffset(
199 FileName, Code.drop_front(MinInsertOffset), Style)),
200 Categories(Style, FileName),
201 IncludeRegex(
llvm::Regex(IncludeRegexPattern)) {
206 Priorities.insert(
Category.Priority);
208 Code.drop_front(MinInsertOffset).split(Lines,
"\n");
210 unsigned Offset = MinInsertOffset;
211 unsigned NextLineOffset;
213 for (
auto Line : Lines) {
214 NextLineOffset =
std::min(Code.size(), Offset +
Line.size() + 1);
215 if (IncludeRegex.match(
Line, &Matches)) {
224 Offset = NextLineOffset;
231 auto Highest = Priorities.begin();
232 if (CategoryEndOffsets.find(*Highest) == CategoryEndOffsets.end()) {
233 if (FirstIncludeOffset >= 0)
234 CategoryEndOffsets[*Highest] = FirstIncludeOffset;
236 CategoryEndOffsets[*Highest] = MinInsertOffset;
242 for (
auto I = ++Priorities.begin(), E = Priorities.end(); I != E; ++I)
243 if (CategoryEndOffsets.find(*I) == CategoryEndOffsets.end())
244 CategoryEndOffsets[*I] = CategoryEndOffsets[*std::prev(I)];
248 void HeaderIncludes::addExistingInclude(Include IncludeToAdd,
249 unsigned NextLineOffset) {
251 ExistingIncludes.try_emplace(trimInclude(IncludeToAdd.Name)).first;
252 Iter->second.push_back(std::move(IncludeToAdd));
253 auto &CurInclude = Iter->second.back();
256 if (CurInclude.R.getOffset() <= MaxInsertOffset) {
258 CurInclude.Name, FirstIncludeOffset < 0);
259 CategoryEndOffsets[Priority] = NextLineOffset;
260 IncludesByPriority[Priority].push_back(&CurInclude);
261 if (FirstIncludeOffset < 0)
262 FirstIncludeOffset = CurInclude.R.getOffset();
268 assert(IncludeName == trimInclude(IncludeName));
272 auto It = ExistingIncludes.find(IncludeName);
273 if (It != ExistingIncludes.end())
274 for (
const auto &Inc : It->second)
275 if ((IsAngled && StringRef(Inc.Name).startswith(
"<")) ||
276 (!IsAngled && StringRef(Inc.Name).startswith(
"\"")))
278 std::string
Quoted = IsAngled ? (
"<" + IncludeName +
">").str()
279 : (
"\"" + IncludeName +
"\"").str();
280 StringRef QuotedName =
Quoted;
282 QuotedName, FirstIncludeOffset < 0);
283 auto CatOffset = CategoryEndOffsets.find(Priority);
284 assert(CatOffset != CategoryEndOffsets.end());
285 unsigned InsertOffset = CatOffset->second;
286 auto Iter = IncludesByPriority.find(Priority);
287 if (Iter != IncludesByPriority.end()) {
288 for (
const auto *Inc : Iter->second) {
289 if (QuotedName < Inc->Name) {
290 InsertOffset = Inc->R.getOffset();
295 assert(InsertOffset <= Code.size());
296 std::string NewInclude = (
"#include " + QuotedName +
"\n").str();
301 if (InsertOffset == Code.size() && (!Code.empty() && Code.back() !=
'\n'))
302 NewInclude =
"\n" + NewInclude;
307 bool IsAngled)
const {
308 assert(IncludeName == trimInclude(IncludeName));
310 auto Iter = ExistingIncludes.find(IncludeName);
311 if (Iter == ExistingIncludes.end())
313 for (
const auto &Inc : Iter->second) {
314 if ((IsAngled && StringRef(Inc.Name).startswith(
"\"")) ||
315 (!IsAngled && StringRef(Inc.Name).startswith(
"<")))
318 FileName, Inc.R.getOffset(), Inc.R.getLength(),
""));
320 auto ErrMsg =
"Unexpected conflicts in #include deletions: " +
322 llvm_unreachable(ErrMsg.c_str());
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Defines the SourceManager interface.
SmallVector< BoundNodes, 1 > match(MatcherT Matcher, const NodeT &Node, ASTContext &Context)
Returns the results of matching Matcher on Node.
const AnnotatedLine * Line
'#include ""' paths, added by 'gcc -iquote'.
Dataflow Directional Tag Classes.
std::string toString(const til::SExpr *E)
__DEVICE__ int min(int __a, int __b)