This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] skip empty lines and comments in the top of the code when inserting new headers.
ClosedPublic

Authored by ioeric on Jun 2 2016, 12:46 AM.

Diff Detail

Repository
rL LLVM

Event Timeline

ioeric updated this revision to Diff 59343.Jun 2 2016, 12:46 AM
ioeric retitled this revision from to [clang-format] skip empty lines and comments in the top of the code when inserting new headers..
ioeric updated this object.
ioeric updated this object.Jun 2 2016, 12:47 AM
ioeric added a reviewer: djasper.
ioeric added subscribers: hokein, cfe-commits.
djasper edited edge metadata.Jun 2 2016, 9:41 AM

Could we very easily use the FormatTokenLexer for this? I.e. find the first non-comment token an start from there?

ioeric updated this revision to Diff 59497.Jun 3 2016, 1:37 AM
ioeric edited edge metadata.
  • Use FormatTokenLexer to skip comments.
djasper added inline comments.Jun 3 2016, 1:52 AM
lib/Format/Format.cpp
1484 ↗(On Diff #59497)

Thinking about this some more, I think the FormatTokenLexer already does too much. Maybe just use a regular lexer?

const SourceManager& SourceMgr = Env->getSourceManager();
Lexer Lex(Env->getFileID(), SourceMgr.getBuffer(Env->getFileID()), SourceMgr,
          getFormattingLangOpts(Style));
Token Tok;
int MinInsertOffset = Code.size();
while (Lex.LexFromRawLexer(&Tok)) {
  if (Tok.isNot(tok::comment)) {
    MinInsertOffset = SourceMgr.getFileOffset(Tok.getLocation());
    break;
  }
}
ioeric updated this revision to Diff 59508.Jun 3 2016, 2:13 AM
  • Use regular Lexer instead of FormatTokenLexer.
ioeric updated this revision to Diff 59510.Jun 3 2016, 2:16 AM
ioeric marked an inline comment as done.
  • Switch back to range-base looping for Lines.
ioeric updated this revision to Diff 59511.Jun 3 2016, 2:21 AM
  • Nits fixed.
djasper accepted this revision.Jun 3 2016, 4:53 AM
djasper edited edge metadata.

Looks good.

This revision is now accepted and ready to land.Jun 3 2016, 4:53 AM
This revision was automatically updated to reflect the committed changes.