Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ bool checkAndConsumeModuleDecl(const SourceManager &SM, Lexer &Lex,
return Matched;
}

// Returns the offset after header guard directives and any comments
// before/after header guards (e.g. #ifndef/#define pair, #pragma once). If no
// header guard is present in the code, this will return the offset after
// skipping all comments from the start of the code.
unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName,
StringRef Code,
const IncludeStyle &Style) {
// Determines the minimum offset into the file where we want to insert header
// includes. This will be put (when available):
// - after `#pragma once`
// - after header guards (`#ifdef` and `#define`)
// - after opening global module (`module;`)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add "after any comments at the start of the file or immediately following one of the above constructs"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

unsigned getMinHeaderInsertionOffset(StringRef FileName, StringRef Code,
const IncludeStyle &Style) {
// \p Consume returns location after header guard or 0 if no header guard is
// found.
auto ConsumeHeaderGuardAndComment =
Expand Down Expand Up @@ -300,8 +300,7 @@ const llvm::Regex HeaderIncludes::IncludeRegex(IncludeRegexPattern);
HeaderIncludes::HeaderIncludes(StringRef FileName, StringRef Code,
const IncludeStyle &Style)
: FileName(FileName), Code(Code), FirstIncludeOffset(-1),
MinInsertOffset(
getOffsetAfterHeaderGuardsAndComments(FileName, Code, Style)),
MinInsertOffset(getMinHeaderInsertionOffset(FileName, Code, Style)),
MaxInsertOffset(MinInsertOffset +
getMaxHeaderInsertionOffset(
FileName, Code.drop_front(MinInsertOffset), Style)),
Expand Down
Loading