clang-format: Fix many Objective-C formatting regressions from r289428
r289428 added a separate language kind for Objective-C, but kept many "Language == LK_Cpp" checks untouched. This introduced a "IsCpp()" method that returns true for both C++ and Objective-C++, and replaces all comparisons of Language with LK_Cpp with calls to this new method. Also add a lot more test coverge for formatting things in LK_ObjC mode, by having FormatTest's verifyFormat() test for LK_ObjC everything that's being tested for LK_Cpp at the moment. Fixes PR32060 and many other things. llvm-svn: 296160
This commit is contained in:
@@ -465,7 +465,7 @@ struct FormatStyle {
|
||||
LK_Java,
|
||||
/// Should be used for JavaScript.
|
||||
LK_JavaScript,
|
||||
/// Should be used for ObjectiveC, ObjectiveC++.
|
||||
/// Should be used for Objective-C, Objective-C++.
|
||||
LK_ObjC,
|
||||
/// Should be used for Protocol Buffers
|
||||
/// (https://developers.google.com/protocol-buffers/).
|
||||
@@ -473,6 +473,7 @@ struct FormatStyle {
|
||||
/// Should be used for TableGen code.
|
||||
LK_TableGen
|
||||
};
|
||||
bool IsCpp() const { return Language == LK_Cpp || Language == LK_ObjC; }
|
||||
|
||||
/// \brief Language, this format style is targeted at.
|
||||
LanguageKind Language;
|
||||
@@ -872,6 +873,8 @@ inline StringRef getLanguageName(FormatStyle::LanguageKind Language) {
|
||||
switch (Language) {
|
||||
case FormatStyle::LK_Cpp:
|
||||
return "C++";
|
||||
case FormatStyle::LK_ObjC:
|
||||
return "Objective-C";
|
||||
case FormatStyle::LK_Java:
|
||||
return "Java";
|
||||
case FormatStyle::LK_JavaScript:
|
||||
|
||||
@@ -156,7 +156,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
|
||||
return true;
|
||||
if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
|
||||
(Previous.is(TT_TemplateCloser) && Current.is(TT_StartOfName) &&
|
||||
Style.Language == FormatStyle::LK_Cpp &&
|
||||
Style.IsCpp() &&
|
||||
// FIXME: This is a temporary workaround for the case where clang-format
|
||||
// sets BreakBeforeParameter to avoid bin packing and this creates a
|
||||
// completely unnecessary line break after a template type that isn't
|
||||
@@ -598,9 +598,7 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
|
||||
// Any break on this level means that the parent level has been broken
|
||||
// and we need to avoid bin packing there.
|
||||
bool NestedBlockSpecialCase =
|
||||
Style.Language != FormatStyle::LK_Cpp &&
|
||||
Style.Language != FormatStyle::LK_ObjC &&
|
||||
Current.is(tok::r_brace) && State.Stack.size() > 1 &&
|
||||
!Style.IsCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 &&
|
||||
State.Stack[State.Stack.size() - 2].NestedBlockInlined;
|
||||
if (!NestedBlockSpecialCase)
|
||||
for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i)
|
||||
|
||||
@@ -1662,7 +1662,7 @@ bool isDeletedHeader(llvm::StringRef HeaderName,
|
||||
tooling::Replacements
|
||||
fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces,
|
||||
const FormatStyle &Style) {
|
||||
if (Style.Language != FormatStyle::LanguageKind::LK_Cpp)
|
||||
if (!Style.IsCpp())
|
||||
return Replaces;
|
||||
|
||||
tooling::Replacements HeaderInsertions;
|
||||
@@ -1864,7 +1864,7 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) {
|
||||
LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
|
||||
LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
|
||||
LangOpts.LineComment = 1;
|
||||
bool AlternativeOperators = Style.Language == FormatStyle::LK_Cpp;
|
||||
bool AlternativeOperators = Style.IsCpp();
|
||||
LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
|
||||
LangOpts.Bool = 1;
|
||||
LangOpts.ObjC1 = 1;
|
||||
|
||||
@@ -560,8 +560,7 @@ FormatToken *FormatTokenLexer::getNextToken() {
|
||||
Column = FormatTok->LastLineColumnWidth;
|
||||
}
|
||||
|
||||
if (Style.Language == FormatStyle::LK_Cpp ||
|
||||
Style.Language == FormatStyle::LK_ObjC) {
|
||||
if (Style.IsCpp()) {
|
||||
if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
|
||||
Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
|
||||
tok::pp_define) &&
|
||||
|
||||
@@ -311,14 +311,13 @@ private:
|
||||
// In C++, this can happen either in array of templates (foo<int>[10])
|
||||
// or when array is a nested template type (unique_ptr<type1<type2>[]>).
|
||||
bool CppArrayTemplates =
|
||||
Style.Language == FormatStyle::LK_Cpp && Parent &&
|
||||
Style.IsCpp() && Parent &&
|
||||
Parent->is(TT_TemplateCloser) &&
|
||||
(Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
|
||||
Contexts.back().InTemplateArgument);
|
||||
|
||||
bool StartsObjCMethodExpr =
|
||||
!CppArrayTemplates && (Style.Language == FormatStyle::LK_Cpp ||
|
||||
Style.Language == FormatStyle::LK_ObjC) &&
|
||||
!CppArrayTemplates && Style.IsCpp() &&
|
||||
Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
|
||||
CurrentToken->isNot(tok::l_brace) &&
|
||||
(!Parent ||
|
||||
@@ -436,9 +435,7 @@ private:
|
||||
if (CurrentToken->isOneOf(tok::colon, tok::l_brace)) {
|
||||
FormatToken *Previous = CurrentToken->getPreviousNonComment();
|
||||
if (((CurrentToken->is(tok::colon) &&
|
||||
(!Contexts.back().ColonIsDictLiteral ||
|
||||
(Style.Language != FormatStyle::LK_Cpp &&
|
||||
Style.Language != FormatStyle::LK_ObjC))) ||
|
||||
(!Contexts.back().ColonIsDictLiteral || !Style.IsCpp())) ||
|
||||
Style.Language == FormatStyle::LK_Proto) &&
|
||||
(Previous->Tok.getIdentifierInfo() ||
|
||||
Previous->is(tok::string_literal)))
|
||||
@@ -1198,9 +1195,7 @@ private:
|
||||
/// \brief Determine whether ')' is ending a cast.
|
||||
bool rParenEndsCast(const FormatToken &Tok) {
|
||||
// C-style casts are only used in C++ and Java.
|
||||
if (Style.Language != FormatStyle::LK_Cpp &&
|
||||
Style.Language != FormatStyle::LK_ObjC &&
|
||||
Style.Language != FormatStyle::LK_Java)
|
||||
if (!Style.IsCpp() && Style.Language != FormatStyle::LK_Java)
|
||||
return false;
|
||||
|
||||
// Empty parens aren't casts and there are no casts at the end of the line.
|
||||
@@ -2211,7 +2206,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
|
||||
const FormatToken &Left = *Right.Previous;
|
||||
if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo())
|
||||
return true; // Never ever merge two identifiers.
|
||||
if (Style.Language == FormatStyle::LK_Cpp) {
|
||||
if (Style.IsCpp()) {
|
||||
if (Left.is(tok::kw_operator))
|
||||
return Right.is(tok::coloncolon);
|
||||
} else if (Style.Language == FormatStyle::LK_Proto) {
|
||||
|
||||
@@ -946,7 +946,7 @@ void UnwrappedLineParser::parseStructuralElement() {
|
||||
if (!parseEnum())
|
||||
break;
|
||||
// This only applies for C++.
|
||||
if (Style.Language != FormatStyle::LK_Cpp) {
|
||||
if (!Style.IsCpp()) {
|
||||
addUnwrappedLine();
|
||||
return;
|
||||
}
|
||||
@@ -1127,7 +1127,7 @@ void UnwrappedLineParser::parseStructuralElement() {
|
||||
}
|
||||
|
||||
bool UnwrappedLineParser::tryToParseLambda() {
|
||||
if (Style.Language != FormatStyle::LK_Cpp) {
|
||||
if (!Style.IsCpp()) {
|
||||
nextToken();
|
||||
return false;
|
||||
}
|
||||
@@ -1737,8 +1737,7 @@ bool UnwrappedLineParser::parseEnum() {
|
||||
nextToken();
|
||||
// If there are two identifiers in a row, this is likely an elaborate
|
||||
// return type. In Java, this can be "implements", etc.
|
||||
if (Style.Language == FormatStyle::LK_Cpp &&
|
||||
FormatTok->is(tok::identifier))
|
||||
if (Style.IsCpp() && FormatTok->is(tok::identifier))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,6 +71,13 @@ protected:
|
||||
void verifyFormat(llvm::StringRef Code,
|
||||
const FormatStyle &Style = getLLVMStyle()) {
|
||||
EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
|
||||
if (Style.Language == FormatStyle::LK_Cpp) {
|
||||
// Objective-C++ is a superset of C++, so everything checked for C++
|
||||
// needs to be checked for Objective-C++ as well.
|
||||
FormatStyle ObjCStyle = Style;
|
||||
ObjCStyle.Language = FormatStyle::LK_ObjC;
|
||||
EXPECT_EQ(Code.str(), format(test::messUp(Code), ObjCStyle));
|
||||
}
|
||||
}
|
||||
|
||||
void verifyIncompleteFormat(llvm::StringRef Code,
|
||||
|
||||
Reference in New Issue
Block a user