/// \brief Indent width for line continuations.
unsigned ContinuationIndentWidth;
+ /// \brief A regular expression that describes comments with special meaning,
+ /// which should not be split into lines or otherwise changed.
+ std::string CommentPragmas;
+
bool operator==(const FormatStyle &R) const {
return AccessModifierOffset == R.AccessModifierOffset &&
ConstructorInitializerIndentWidth ==
SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses &&
SpaceBeforeParens == R.SpaceBeforeParens &&
SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators &&
- ContinuationIndentWidth == R.ContinuationIndentWidth;
+ ContinuationIndentWidth == R.ContinuationIndentWidth &&
+ CommentPragmas == R.CommentPragmas;
}
};
bool BinPackInconclusiveFunctions)
: Style(Style), SourceMgr(SourceMgr), Whitespaces(Whitespaces),
Encoding(Encoding),
- BinPackInconclusiveFunctions(BinPackInconclusiveFunctions) {}
+ BinPackInconclusiveFunctions(BinPackInconclusiveFunctions),
+ CommentPragmasRegex(Style.CommentPragmas) {}
LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
const AnnotatedLine *Line,
return 0;
}
} else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) {
+ if (CommentPragmasRegex.match(Current.TokenText.substr(2)))
+ return 0;
Token.reset(new BreakableBlockComment(
Current, State.Line->Level, StartColumn, Current.OriginalColumn,
!Current.Previous, State.Line->InPPDirective, Encoding, Style));
} else if (Current.Type == TT_LineComment &&
(Current.Previous == NULL ||
Current.Previous->Type != TT_ImplicitStringLiteral)) {
+ if (CommentPragmasRegex.match(Current.TokenText.substr(2)))
+ return 0;
Token.reset(new BreakableLineComment(Current, State.Line->Level,
StartColumn, /*InPPDirective=*/false,
Encoding, Style));
#include "Encoding.h"
#include "clang/Format/Format.h"
+#include "llvm/Support/Regex.h"
namespace clang {
class SourceManager;
WhitespaceManager &Whitespaces;
encoding::Encoding Encoding;
bool BinPackInconclusiveFunctions;
+ llvm::Regex CommentPragmasRegex;
};
struct ParenState {
IO.mapOptional("SpaceBeforeAssignmentOperators",
Style.SpaceBeforeAssignmentOperators);
IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
+ IO.mapOptional("CommentPragmas", Style.CommentPragmas);
// For backward compatibility.
if (!IO.outputting()) {
LLVMStyle.SpaceBeforeAssignmentOperators = true;
LLVMStyle.ContinuationIndentWidth = 4;
LLVMStyle.SpacesInAngles = false;
+ LLVMStyle.CommentPragmas = "^ IWYU pragma:";
LLVMStyle.PenaltyBreakComment = 300;
LLVMStyle.PenaltyBreakFirstLessLess = 120;
getLLVMStyleWithColumns(49)));
}
+TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
+ FormatStyle Pragmas = getLLVMStyleWithColumns(30);
+ Pragmas.CommentPragmas = "^ IWYU pragma:";
+ EXPECT_EQ(
+ "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
+ format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
+ EXPECT_EQ(
+ "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
+ format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
+}
+
TEST_F(FormatTest, PriorityOfCommentBreaking) {
EXPECT_EQ("if (xxx ==\n"
" yyy && // aaaaaaaaaaaa bbbbbbbbb\n"