clang-format does not lex the ISO 646 alternative operator keywords as
operators; rather, it interprets them as normal identifiers.
This would result in code such as `(a) and (b)` getting reformatted as
`(a)and(b)`.
As a result of some discussion on cfe-dev, this patch changes the lexer option
used by clang-format to interpret these keywords as operators and not
identifiers.
http://reviews.llvm.org/D3634
Files:
include/clang/Format/Format.h
lib/Format/Format.cpp
tools/clang-format/ClangFormat.cpp
unittests/Format/FormatTest.cpp
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -458,10 +458,14 @@
/// \brief Returns the \c LangOpts that the formatter expects you to set.
///
+/// \param Lang determines the lexing mode: LK_Cpp will cause the lexer to
+/// interpret the C alternative operator tokens as operators and not
+/// identifiers.
/// \param Standard determines lexing mode: LC_Cpp11 and LS_Auto turn on C++11
/// lexing mode, LS_Cpp03 - C++03 mode.
-LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard =
- FormatStyle::LS_Cpp11);
+LangOptions getFormattingLangOpts(
+ FormatStyle::LanguageKind Lang,
+ FormatStyle::LanguageStandard Standard = FormatStyle::LS_Cpp11);
/// \brief Description to be used for help text for a llvm::cl option for
/// specifying format style. The description is closely related to the operation
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -1180,7 +1180,7 @@
encoding::Encoding Encoding)
: FormatTok(NULL), IsFirstToken(true), GreaterStashed(false), Column(0),
TrailingWhitespace(0), Lex(Lex), SourceMgr(SourceMgr), Style(Style),
- IdentTable(getFormattingLangOpts()), Encoding(Encoding),
+ IdentTable(getFormattingLangOpts(Style.Language)), Encoding(Encoding),
FirstInLineIndex(0) {
Lex.SetKeepWhitespaceMode(true);
@@ -1835,7 +1835,7 @@
FileID ID =
SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr,
- getFormattingLangOpts(Style.Standard));
+ getFormattingLangOpts(Style.Language, Style.Standard));
SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
std::vector<CharSourceRange> CharRanges;
for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
@@ -1846,12 +1846,14 @@
return reformat(Style, Lex, SourceMgr, CharRanges);
}
-LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) {
+LangOptions getFormattingLangOpts(FormatStyle::LanguageKind Lang,
+ FormatStyle::LanguageStandard Standard) {
LangOptions LangOpts;
LangOpts.CPlusPlus = 1;
LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
LangOpts.CPlusPlus1y = Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
LangOpts.LineComment = 1;
+ LangOpts.CXXOperatorNames = FormatStyle::LK_Cpp == Lang ? 1 : 0;
LangOpts.Bool = 1;
LangOpts.ObjC1 = 1;
LangOpts.ObjC2 = 1;
Index: tools/clang-format/ClangFormat.cpp
===================================================================
--- tools/clang-format/ClangFormat.cpp
+++ tools/clang-format/ClangFormat.cpp
@@ -223,7 +223,7 @@
FormatStyle FormatStyle = getStyle(
Style, (FileName == "-") ? AssumeFilename : FileName, FallbackStyle);
Lexer Lex(ID, Sources.getBuffer(ID), Sources,
- getFormattingLangOpts(FormatStyle.Standard));
+ getFormattingLangOpts(FormatStyle.Language, FormatStyle.Standard));
tooling::Replacements Replaces = reformat(FormatStyle, Lex, Sources, Ranges);
if (OutputXML) {
llvm::outs()
@@ -246,8 +246,8 @@
return true;
} else {
if (Cursor.getNumOccurrences() != 0)
- outs() << "{ \"Cursor\": " << tooling::shiftedCodePosition(
- Replaces, Cursor) << " }\n";
+ outs() << "{ \"Cursor\": "
+ << tooling::shiftedCodePosition(Replaces, Cursor) << " }\n";
Rewrite.getEditBuffer(ID).write(outs());
}
}
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -270,6 +270,17 @@
9, 5, getLLVMStyle()));
}
+TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
+ verifyFormat("x = (a) and (b);");
+ verifyFormat("x = (a) or (b);");
+ verifyFormat("x = (a) bitand (b);");
+ verifyFormat("x = (a) bitor (b);");
+ verifyFormat("x = (a) not_eq (b);");
+ verifyFormat("x = (a) and_eq (b);");
+ verifyFormat("x = (a) or_eq (b);");
+ verifyFormat("x = (a) xor (b);");
+}
+
//===----------------------------------------------------------------------===//
// Tests for control statements.
//===----------------------------------------------------------------------===//
@@ -2844,9 +2855,21 @@
"bool aaaaaaa =\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
" bbbbbbbb();");
+ verifyFormat(
+ "bool aaaaaaa =\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
+ " bbbbbbbb();");
+
verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
" ccccccccc == ddddddddddd;");
+ verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
+ " ccccccccc == ddddddddddd;");
+ verifyFormat(
+ "bool aaaaaaaaaaaaaaaaaaaaa =\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
+ " ccccccccc == ddddddddddd;");
verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
" aaaaaa) &&\n"
@@ -3592,20 +3615,42 @@
verifyFormat(
"if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
" bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
+ verifyFormat(
+ "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
+
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
" ccccccccccccccccccccccccc) {\n}");
+ verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
+ " ccccccccccccccccccccccccc) {\n}");
+
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
" ccccccccccccccccccccccccc) {\n}");
+ verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
+ " ccccccccccccccccccccccccc) {\n}");
+
verifyFormat(
"if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
" ccccccccccccccccccccccccc) {\n}");
+ verifyFormat(
+ "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
+ " ccccccccccccccccccccccccc) {\n}");
+
verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
" bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
" cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
" dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
+ verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
+ " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
+ " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
+ " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
+
verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
" aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
" aaaaaaaaaaaaaaa != aa) {\n}");
+ verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
+ " aaaaaaaaaaaaaaa != aa) {\n}");
}
TEST_F(FormatTest, BreaksAfterAssignments) {
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits