Author: owenca Date: 2022-08-21T18:23:14-07:00 New Revision: 41214456de9d23b2cf6f87cb39b1c79682d799b7
URL: https://github.com/llvm/llvm-project/commit/41214456de9d23b2cf6f87cb39b1c79682d799b7 DIFF: https://github.com/llvm/llvm-project/commit/41214456de9d23b2cf6f87cb39b1c79682d799b7.diff LOG: [clang-format] Fix BeforeHash indent of comments above PPDirective Fixes #56326. Differential Revision: https://reviews.llvm.org/D132097 Added: Modified: clang/lib/Format/TokenAnnotator.cpp clang/lib/Format/TokenAnnotator.h clang/lib/Format/UnwrappedLineFormatter.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index b4694a59a89e2..668f8b3f431ce 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2696,15 +2696,18 @@ void TokenAnnotator::setCommentLineLevels( NextNonCommentLine->First->NewlinesBefore <= 1 && NextNonCommentLine->First->OriginalColumn == Line->First->OriginalColumn) { + const bool PPDirectiveOrImportStmt = + NextNonCommentLine->Type == LT_PreprocessorDirective || + NextNonCommentLine->Type == LT_ImportStatement; + if (PPDirectiveOrImportStmt) + Line->Type = LT_CommentAbovePPDirective; // Align comments for preprocessor lines with the # in column 0 if // preprocessor lines are not indented. Otherwise, align with the next // line. - Line->Level = - (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash && - (NextNonCommentLine->Type == LT_PreprocessorDirective || - NextNonCommentLine->Type == LT_ImportStatement)) - ? 0 - : NextNonCommentLine->Level; + Line->Level = Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash && + PPDirectiveOrImportStmt + ? 0 + : NextNonCommentLine->Level; } else { NextNonCommentLine = Line->First->isNot(tok::r_brace) ? Line : nullptr; } diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h index 0e485ee9f0b4f..028ff6d2ccb2c 100644 --- a/clang/lib/Format/TokenAnnotator.h +++ b/clang/lib/Format/TokenAnnotator.h @@ -31,6 +31,7 @@ enum LineType { LT_PreprocessorDirective, LT_VirtualFunctionDecl, LT_ArrayOfStructInitializer, + LT_CommentAbovePPDirective, }; class AnnotatedLine { diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 9fb02eb8acb7c..af88c2722b22d 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -60,7 +60,9 @@ class LevelIndentTracker { // Update the indent level cache size so that we can rely on it // having the right size in adjustToUnmodifiedline. skipLine(Line, /*UnknownIndent=*/true); - if (Line.InPPDirective) { + if (Line.InPPDirective || + (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash && + Line.Type == LT_CommentAbovePPDirective)) { unsigned IndentWidth = (Style.PPIndentWidth >= 0) ? Style.PPIndentWidth : Style.IndentWidth; Indent = Line.Level * IndentWidth + AdditionalIndent; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index c3264e7cf84a2..83aa0dd9fb7a7 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -4954,6 +4954,25 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { " int y = 0;\n" "}\n", style); + verifyFormat("#if 1\n" + " // some comments\n" + " // another\n" + " #define foo 1\n" + "// not a define comment\n" + "void bar() {\n" + " // comment\n" + " int y = 0;\n" + "}", + "#if 1\n" + "// some comments\n" + "// another\n" + "#define foo 1\n" + "// not a define comment\n" + "void bar() {\n" + " // comment\n" + " int y = 0;\n" + "}", + style); } TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits