LangOptions createLangOpts() {
23LangOpts.CPlusPlus = 1;
24LangOpts.CPlusPlus11 = 1;
25LangOpts.CPlusPlus14 = 1;
26LangOpts.LineComment = 1;
27LangOpts.CXXOperatorNames = 1;
30LangOpts.MicrosoftExt = 1;
31LangOpts.DeclSpecKeyword = 1;
40unsignedgetOffsetAfterTokenSequence(
41StringRef
FileName, StringRef Code,
constIncludeStyle &Style,
42llvm::function_ref<
unsigned(
constSourceManager &, Lexer &, Token &)>
43GetOffsetAfterSequence) {
44SourceManagerForFile VirtualSM(
FileName, Code);
45SourceManager &
SM= VirtualSM.get();
46LangOptions LangOpts = createLangOpts();
47Lexer Lex(
SM.getMainFileID(),
SM.getBufferOrFake(
SM.getMainFileID()),
SM,
51Lex.LexFromRawLexer(Tok);
52 returnGetOffsetAfterSequence(
SM, Lex, Tok);
59boolcheckAndConsumeDirectiveWithName(
60Lexer &Lex, StringRef Name, Token &Tok,
61std::optional<StringRef> RawIDName = std::nullopt) {
62 boolMatched = Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&
63Tok.is(tok::raw_identifier) &&
64Tok.getRawIdentifier() == Name && !Lex.LexFromRawLexer(Tok) &&
65Tok.is(tok::raw_identifier) &&
66(!RawIDName || Tok.getRawIdentifier() == *RawIDName);
68Lex.LexFromRawLexer(Tok);
72voidskipComments(Lexer &Lex, Token &Tok) {
73 while(Tok.is(tok::comment))
74 if(Lex.LexFromRawLexer(Tok))
82unsignedgetOffsetAfterHeaderGuardsAndComments(StringRef
FileName,
84 constIncludeStyle &Style) {
87 autoConsumeHeaderGuardAndComment =
88[&](std::function<
unsigned(
constSourceManager &
SM, Lexer &Lex,
91 returngetOffsetAfterTokenSequence(
93[&Consume](
constSourceManager &
SM, Lexer &Lex, Token Tok) {
94skipComments(Lex, Tok);
95 unsignedInitialOffset =
SM.getFileOffset(Tok.getLocation());
96 returnstd::max(InitialOffset, Consume(
SM, Lex, Tok));
101ConsumeHeaderGuardAndComment(
102[](
constSourceManager &
SM, Lexer &Lex, Token Tok) ->
unsigned{
103 if(checkAndConsumeDirectiveWithName(Lex,
"ifndef", Tok)) {
104skipComments(Lex, Tok);
105 if(checkAndConsumeDirectiveWithName(Lex,
"define", Tok) &&
106Tok.isAtStartOfLine())
107 return SM.getFileOffset(Tok.getLocation());
112ConsumeHeaderGuardAndComment(
113[](
constSourceManager &
SM, Lexer &Lex, Token Tok) ->
unsigned{
114 if(checkAndConsumeDirectiveWithName(Lex,
"pragma", Tok,
115StringRef(
"once")))
116 return SM.getFileOffset(Tok.getLocation());
125boolcheckAndConsumeInclusiveDirective(Lexer &Lex, Token &Tok) {
126 autoMatched = [&]() {
127Lex.LexFromRawLexer(Tok);
130 if(Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&
131Tok.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))
159unsignedgetMaxHeaderInsertionOffset(StringRef
FileName, StringRef Code,
160 constIncludeStyle &Style) {
161 returngetOffsetAfterTokenSequence(
163[](
constSourceManager &
SM, Lexer &Lex, Token Tok) {
164skipComments(Lex, Tok);
165 unsigned MaxOffset=
SM.getFileOffset(Tok.getLocation());
166 while(checkAndConsumeInclusiveDirective(Lex, Tok))
172inlineStringRef trimInclude(StringRef IncludeName) {
173 returnIncludeName.trim(
"\"<>");
176const charIncludeRegexPattern[] =
177R
"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))"; 184StringRef matchingStem(llvm::StringRef
Path) {
185StringRef Name = llvm::sys::path::filename(
Path);
186 returnName.substr(0, Name.find(
'.', 1));
194 for(
const auto&
Category: Style.IncludeCategories) {
195CategoryRegexs.emplace_back(
Category.Regex,
Category.RegexIsCaseSensitive
196? llvm::Regex::NoFlags
197: llvm::Regex::IgnoreCase);
203 if(!Style.IncludeIsMainSourceRegex.empty()) {
204llvm::Regex MainFileRegex(Style.IncludeIsMainSourceRegex);
205IsMainFile |= MainFileRegex.match(
FileName);
210 boolCheckMainHeader)
const{
212 for(
unsignedi = 0, e = CategoryRegexs.size(); i != e; ++i)
213 if(CategoryRegexs[i].
match(IncludeName)) {
214Ret = Style.IncludeCategories[i].Priority;
217 if(CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName))
223 boolCheckMainHeader)
const{
225 for(
unsignedi = 0, e = CategoryRegexs.size(); i != e; ++i)
226 if(CategoryRegexs[i].
match(IncludeName)) {
227Ret = Style.IncludeCategories[i].SortPriority;
229Ret = Style.IncludeCategories[i].Priority;
232 if(CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName))
236boolIncludeCategoryManager::isMainHeader(StringRef IncludeName)
const{
237 switch(Style.MainIncludeChar) {
239 if(!IncludeName.starts_with(
"\""))
243 if(!IncludeName.starts_with(
"<"))
251IncludeName.drop_front(1).drop_back(1);
254StringRef HeaderStem = llvm::sys::path::stem(IncludeName);
255StringRef FileStem = llvm::sys::path::stem(FileName);
256StringRef MatchingFileStem = matchingStem(FileName);
266 if(MatchingFileStem.starts_with_insensitive(HeaderStem))
267Matching = MatchingFileStem;
268 else if(FileStem.equals_insensitive(HeaderStem))
270 if(!Matching.empty()) {
271llvm::Regex MainIncludeRegex(HeaderStem.str() + Style.IncludeIsMainRegex,
272llvm::Regex::IgnoreCase);
273 if(MainIncludeRegex.match(Matching))
285getOffsetAfterHeaderGuardsAndComments(
FileName, Code, Style)),
286MaxInsertOffset(MinInsertOffset +
287getMaxHeaderInsertionOffset(
288 FileName, Code.drop_front(MinInsertOffset), Style)),
289MainIncludeFound(
false),
294 for(
const auto&
Category: Style.IncludeCategories)
295Priorities.insert(
Category.Priority);
297Code.drop_front(MinInsertOffset).split(Lines,
"\n");
299 unsignedOffset = MinInsertOffset;
300 unsignedNextLineOffset;
302 for(
auto Line: Lines) {
303NextLineOffset = std::min(Code.size(), Offset +
Line.size() + 1);
310Offset, std::min(
Line.size() + 1, Code.size() - Offset)),
315Offset = NextLineOffset;
322 autoHighest = Priorities.begin();
323 auto[It, Inserted] = CategoryEndOffsets.try_emplace(*Highest);
325It->second = FirstIncludeOffset >= 0 ? FirstIncludeOffset : MinInsertOffset;
330 for(
autoI = ++Priorities.begin(),
E= Priorities.end(); I !=
E; ++I)
331 if(CategoryEndOffsets.find(*I) == CategoryEndOffsets.end())
332CategoryEndOffsets[*I] = CategoryEndOffsets[*std::prev(I)];
336voidHeaderIncludes::addExistingInclude(
IncludeIncludeToAdd,
337 unsignedNextLineOffset) {
338 auto&Incs = ExistingIncludes[trimInclude(IncludeToAdd.Name)];
339Incs.push_back(std::move(IncludeToAdd));
340 auto&CurInclude = Incs.back();
343 if(CurInclude.R.getOffset() <= MaxInsertOffset) {
345CurInclude.Name,
!MainIncludeFound);
347MainIncludeFound =
true;
348CategoryEndOffsets[
Priority] = NextLineOffset;
349IncludesByPriority[
Priority].push_back(&CurInclude);
350 if(FirstIncludeOffset < 0)
351FirstIncludeOffset = CurInclude.R.getOffset();
355std::optional<tooling::Replacement>
358assert(IncludeName == trimInclude(IncludeName));
362 autoIt = ExistingIncludes.find(IncludeName);
363 if(It != ExistingIncludes.end()) {
364 for(
const auto&Inc : It->second)
366((IsAngled && StringRef(Inc.Name).starts_with(
"<")) ||
367(!IsAngled && StringRef(Inc.Name).starts_with(
"\""))))
371std::string(llvm::formatv(IsAngled ?
"<{0}>":
"\"{0}\"", IncludeName));
372StringRef QuotedName = Quoted;
374QuotedName,
!MainIncludeFound);
375 autoCatOffset = CategoryEndOffsets.find(
Priority);
376assert(CatOffset != CategoryEndOffsets.end());
377 unsignedInsertOffset = CatOffset->second;
379 if(
Iter!= IncludesByPriority.end()) {
380 for(
const auto*Inc :
Iter->second) {
381 if(QuotedName < Inc->Name) {
382InsertOffset = Inc->R.getOffset();
387assert(InsertOffset <= Code.size());
388llvm::StringRef DirectiveSpelling =
390std::string NewInclude =
391llvm::formatv(
"#{0} {1}\n", DirectiveSpelling, QuotedName);
396 if(InsertOffset == Code.size() && (!Code.empty() && Code.back() !=
'\n'))
397NewInclude =
"\n"+ NewInclude;
402 boolIsAngled)
const{
403assert(IncludeName == trimInclude(IncludeName));
405 auto Iter= ExistingIncludes.find(IncludeName);
406 if(
Iter== ExistingIncludes.end())
408 for(
const auto&Inc :
Iter->second) {
409 if((IsAngled && StringRef(Inc.Name).starts_with(
"\"")) ||
410(!IsAngled && StringRef(Inc.Name).starts_with(
"<")))
413 FileName, Inc.R.getOffset(), Inc.R.getLength(),
""));
415 autoErrMsg =
"Unexpected conflicts in #include deletions: "+
416llvm::toString(std::move(Err));
417llvm_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.
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4