r271664 - [clang-format] skip empty lines and comments in the top of the code when inserting new headers.
Eric Liu via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 3 05:53:01 PDT 2016
Author: ioeric
Date: Fri Jun 3 07:52:59 2016
New Revision: 271664
URL: http://llvm.org/viewvc/llvm-project?rev=271664&view=rev
Log:
[clang-format] skip empty lines and comments in the top of the code when inserting new headers.
Summary:
[clang-format] skip empty lines and comments in the top of the code when inserting new headers.
Pair-programmed with @hokein
Reviewers: djasper
Subscribers: ioeric, cfe-commits, hokein, klimek
Differential Revision: http://reviews.llvm.org/D20898
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/CleanupTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=271664&r1=271663&r2=271664&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Fri Jun 3 07:52:59 2016
@@ -1469,6 +1469,20 @@ fixCppIncludeInsertions(StringRef Code,
StringRef FileName = Replaces.begin()->getFilePath();
IncludeCategoryManager Categories(Style, FileName);
+ std::unique_ptr<Environment> Env =
+ Environment::CreateVirtualEnvironment(Code, FileName, /*Ranges=*/{});
+ const SourceManager &SourceMgr = Env->getSourceManager();
+ Lexer Lex(Env->getFileID(), SourceMgr.getBuffer(Env->getFileID()), SourceMgr,
+ getFormattingLangOpts(Style));
+ Token Tok;
+ // All new headers should be inserted after this offset.
+ int MinInsertOffset = Code.size();
+ while (!Lex.LexFromRawLexer(Tok)) {
+ if (Tok.isNot(tok::comment)) {
+ MinInsertOffset = SourceMgr.getFileOffset(Tok.getLocation());
+ break;
+ }
+ }
// Record the offset of the end of the last include in each category.
std::map<int, int> CategoryEndOffsets;
// All possible priorities.
@@ -1477,10 +1491,11 @@ fixCppIncludeInsertions(StringRef Code,
for (const auto &Category : Style.IncludeCategories)
Priorities.insert(Category.Priority);
int FirstIncludeOffset = -1;
- int Offset = 0;
- int AfterHeaderGuard = 0;
+ bool HeaderGuardFound = false;
+ StringRef TrimmedCode = Code.drop_front(MinInsertOffset);
SmallVector<StringRef, 32> Lines;
- Code.split(Lines, '\n');
+ TrimmedCode.split(Lines, '\n');
+ int Offset = MinInsertOffset;
for (auto Line : Lines) {
if (IncludeRegex.match(Line, &Matches)) {
StringRef IncludeName = Matches[2];
@@ -1492,22 +1507,22 @@ fixCppIncludeInsertions(StringRef Code,
}
Offset += Line.size() + 1;
// FIXME: make header guard matching stricter, e.g. consider #ifndef.
- if (AfterHeaderGuard == 0 && DefineRegex.match(Line))
- AfterHeaderGuard = Offset;
+ if (!HeaderGuardFound && DefineRegex.match(Line)) {
+ HeaderGuardFound = true;
+ MinInsertOffset = Offset;
+ }
}
// Populate CategoryEndOfssets:
// - Ensure that CategoryEndOffset[Highest] is always populated.
// - If CategoryEndOffset[Priority] isn't set, use the next higher value that
// is set, up to CategoryEndOffset[Highest].
- // FIXME: skip comment section in the beginning of the code if there is no
- // existing #include and #define.
auto Highest = Priorities.begin();
if (CategoryEndOffsets.find(*Highest) == CategoryEndOffsets.end()) {
if (FirstIncludeOffset >= 0)
CategoryEndOffsets[*Highest] = FirstIncludeOffset;
else
- CategoryEndOffsets[*Highest] = AfterHeaderGuard;
+ CategoryEndOffsets[*Highest] = MinInsertOffset;
}
// By this point, CategoryEndOffset[Highest] is always set appropriately:
// - to an appropriate location before/after existing #includes, or
Modified: cfe/trunk/unittests/Format/CleanupTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/CleanupTest.cpp?rev=271664&r1=271663&r2=271664&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/CleanupTest.cpp (original)
+++ cfe/trunk/unittests/Format/CleanupTest.cpp Fri Jun 3 07:52:59 2016
@@ -465,13 +465,13 @@ TEST_F(CleanUpReplacementsTest, InsertMu
TEST_F(CleanUpReplacementsTest, InsertMultipleNewHeadersAndSortLLVM) {
std::string Code = "\nint x;";
- std::string Expected = "#include \"fix.h\"\n"
+ std::string Expected = "\n#include \"fix.h\"\n"
"#include \"a.h\"\n"
"#include \"b.h\"\n"
"#include \"c.h\"\n"
"#include <list>\n"
"#include <vector>\n"
- "\nint x;";
+ "int x;";
tooling::Replacements Replaces = {createInsertion("#include \"a.h\""),
createInsertion("#include \"c.h\""),
createInsertion("#include \"b.h\""),
@@ -483,13 +483,13 @@ TEST_F(CleanUpReplacementsTest, InsertMu
TEST_F(CleanUpReplacementsTest, InsertMultipleNewHeadersAndSortGoogle) {
std::string Code = "\nint x;";
- std::string Expected = "#include \"fix.h\"\n"
+ std::string Expected = "\n#include \"fix.h\"\n"
"#include <list>\n"
"#include <vector>\n"
"#include \"a.h\"\n"
"#include \"b.h\"\n"
"#include \"c.h\"\n"
- "\nint x;";
+ "int x;";
tooling::Replacements Replaces = {createInsertion("#include \"a.h\""),
createInsertion("#include \"c.h\""),
createInsertion("#include \"b.h\""),
@@ -502,21 +502,22 @@ TEST_F(CleanUpReplacementsTest, InsertMu
TEST_F(CleanUpReplacementsTest, FormatCorrectLineWhenHeadersAreInserted) {
std::string Code = "\n"
+ "int x;\n"
"int a;\n"
"int a;\n"
"int a;";
- std::string Expected = "#include \"x.h\"\n"
+ std::string Expected = "\n#include \"x.h\"\n"
"#include \"y.h\"\n"
"#include \"clang/x/x.h\"\n"
"#include <list>\n"
"#include <vector>\n"
- "\n"
+ "int x;\n"
"int a;\n"
"int b;\n"
"int a;";
tooling::Replacements Replaces = {
- createReplacement(getOffset(Code, 3, 8), 1, "b"),
+ createReplacement(getOffset(Code, 4, 8), 1, "b"),
createInsertion("#include <vector>"),
createInsertion("#include <list>"),
createInsertion("#include \"clang/x/x.h\""),
@@ -537,6 +538,76 @@ TEST_F(CleanUpReplacementsTest, NotConfu
EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
}
+TEST_F(CleanUpReplacementsTest, SkippedTopComment) {
+ std::string Code = "// comment\n"
+ "\n"
+ " // comment\n";
+ std::string Expected = "// comment\n"
+ "\n"
+ " // comment\n"
+ "#include <vector>\n";
+ tooling::Replacements Replaces = {createInsertion("#include <vector>")};
+ EXPECT_EQ(Expected, apply(Code, Replaces));
+}
+
+TEST_F(CleanUpReplacementsTest, SkippedMixedComments) {
+ std::string Code = "// comment\n"
+ "// comment \\\n"
+ " comment continued\n"
+ "/*\n"
+ "* comment\n"
+ "*/\n";
+ std::string Expected = "// comment\n"
+ "// comment \\\n"
+ " comment continued\n"
+ "/*\n"
+ "* comment\n"
+ "*/\n"
+ "#include <vector>\n";
+ tooling::Replacements Replaces = {createInsertion("#include <vector>")};
+ EXPECT_EQ(Expected, apply(Code, Replaces));
+}
+
+TEST_F(CleanUpReplacementsTest, MultipleBlockCommentsInOneLine) {
+ std::string Code = "/*\n"
+ "* comment\n"
+ "*/ /* comment\n"
+ "*/\n"
+ "\n\n"
+ "/* c1 */ /*c2 */\n";
+ std::string Expected = "/*\n"
+ "* comment\n"
+ "*/ /* comment\n"
+ "*/\n"
+ "\n\n"
+ "/* c1 */ /*c2 */\n"
+ "#include <vector>\n";
+ tooling::Replacements Replaces = {createInsertion("#include <vector>")};
+ EXPECT_EQ(Expected, apply(Code, Replaces));
+}
+
+TEST_F(CleanUpReplacementsTest, CodeAfterComments) {
+ std::string Code = "/*\n"
+ "* comment\n"
+ "*/ /* comment\n"
+ "*/\n"
+ "\n\n"
+ "/* c1 */ /*c2 */\n"
+ "\n"
+ "int x;\n";
+ std::string Expected = "/*\n"
+ "* comment\n"
+ "*/ /* comment\n"
+ "*/\n"
+ "\n\n"
+ "/* c1 */ /*c2 */\n"
+ "\n"
+ "#include <vector>\n"
+ "int x;\n";
+ tooling::Replacements Replaces = {createInsertion("#include <vector>")};
+ EXPECT_EQ(Expected, apply(Code, Replaces));
+}
+
} // end namespace
} // end namespace format
} // end namespace clang
More information about the cfe-commits
mailing list