[clang] bcc1dee - [clang-format] Add StatementAttributeLikeMacros option
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Sun Jan 17 22:02:01 PST 2021
Author: Björn Schäpers
Date: 2021-01-18T06:54:31+01:00
New Revision: bcc1dee60019f3a488a04dc7f701f7a692040fed
URL: https://github.com/llvm/llvm-project/commit/bcc1dee60019f3a488a04dc7f701f7a692040fed
DIFF: https://github.com/llvm/llvm-project/commit/bcc1dee60019f3a488a04dc7f701f7a692040fed.diff
LOG: [clang-format] Add StatementAttributeLikeMacros option
This allows to ignore for example Qts emit when
AlignConsecutiveDeclarations is set, otherwise it is parsed as a type
and it results in some misformating:
unsigned char MyChar = 'x';
emit signal(MyChar);
Differential Revision: https://reviews.llvm.org/D93776
Added:
Modified:
clang/docs/ClangFormatStyleOptions.rst
clang/docs/ReleaseNotes.rst
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/lib/Format/FormatToken.h
clang/lib/Format/FormatTokenLexer.cpp
clang/lib/Format/TokenAnnotator.cpp
clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 8eee6187d0c6..928d136ef9ff 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -3074,6 +3074,20 @@ the configuration (without a prefix: ``Auto``).
+**StatementAttributeLikeMacros** (``std::vector<std::string>``)
+ Macros which are ignored in front of a statement, as if they were an
+ attribute. So that they are not parsed as identifier, for example for Qts
+ emit. \code
+ AlignConsecutiveDeclarations: true
+ StatementAttributeLikeMacros: []
+ unsigned char data = 'x';
+ emit signal(data); // This is parsed as variable declaration.
+
+ AlignConsecutiveDeclarations: true
+ StatementAttributeLikeMacros: [emit]
+ unsigned char data = 'x';
+ emit signal(data); // Now it's fine again.
+
**StatementMacros** (``std::vector<std::string>``)
A vector of macros that should be interpreted as complete
statements.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 76d4b6bb5acd..7fcae5bce164 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -296,6 +296,10 @@ clang-format
- Option ``SpaceBeforeCaseColon`` has been added to add a space before the
colon in a case or default statement.
+- Option ``StatementAttributeLikeMacros`` has been added to declare
+ macros which are not parsed as a type in front of a statement. See
+ the documentation for an example.
+
libclang
--------
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 6b3fb8164a28..943a33cee4fd 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2676,6 +2676,21 @@ struct FormatStyle {
/// \endcode
LanguageStandard Standard;
+ /// Macros which are ignored in front of a statement, as if they were an
+ /// attribute. So that they are not parsed as identifier, for example for Qts
+ /// emit. \code
+ /// AlignConsecutiveDeclarations: true
+ /// StatementAttributeLikeMacros: []
+ /// unsigned char data = 'x';
+ /// emit signal(data); // This is parsed as variable declaration.
+ ///
+ /// AlignConsecutiveDeclarations: true
+ /// StatementAttributeLikeMacros: [emit]
+ /// unsigned char data = 'x';
+ /// emit signal(data); // Now it's fine again.
+ /// \endcode
+ std::vector<std::string> StatementAttributeLikeMacros;
+
/// The number of columns used for tab stops.
unsigned TabWidth;
@@ -2825,9 +2840,11 @@ struct FormatStyle {
SpacesInSquareBrackets == R.SpacesInSquareBrackets &&
SpaceBeforeSquareBrackets == R.SpaceBeforeSquareBrackets &&
BitFieldColonSpacing == R.BitFieldColonSpacing &&
- Standard == R.Standard && TabWidth == R.TabWidth &&
- StatementMacros == R.StatementMacros && UseTab == R.UseTab &&
- UseCRLF == R.UseCRLF && TypenameMacros == R.TypenameMacros;
+ Standard == R.Standard &&
+ StatementAttributeLikeMacros == R.StatementAttributeLikeMacros &&
+ StatementMacros == R.StatementMacros && TabWidth == R.TabWidth &&
+ UseTab == R.UseTab && UseCRLF == R.UseCRLF &&
+ TypenameMacros == R.TypenameMacros;
}
llvm::Optional<FormatStyle> GetLanguageStyle(LanguageKind Language) const;
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 1207ac2dcc2b..9f007819326c 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -549,6 +549,8 @@ template <> struct MappingTraits<FormatStyle> {
Style.ExperimentalAutoDetectBinPacking);
IO.mapOptional("FixNamespaceComments", Style.FixNamespaceComments);
IO.mapOptional("ForEachMacros", Style.ForEachMacros);
+ IO.mapOptional("StatementAttributeLikeMacros",
+ Style.StatementAttributeLikeMacros);
IO.mapOptional("IncludeBlocks", Style.IncludeStyle.IncludeBlocks);
IO.mapOptional("IncludeCategories", Style.IncludeStyle.IncludeCategories);
IO.mapOptional("IncludeIsMainRegex", Style.IncludeStyle.IncludeIsMainRegex);
@@ -983,6 +985,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.SortIncludes = true;
LLVMStyle.SortJavaStaticImport = FormatStyle::SJSIO_Before;
LLVMStyle.SortUsingDeclarations = true;
+ LLVMStyle.StatementAttributeLikeMacros.push_back("Q_EMIT");
LLVMStyle.StatementMacros.push_back("Q_UNUSED");
LLVMStyle.StatementMacros.push_back("QT_REQUIRE_VERSION");
LLVMStyle.WhitespaceSensitiveMacros.push_back("STRINGIZE");
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index a6d7102f0ac3..2f53b338379d 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -96,6 +96,7 @@ namespace format {
TYPE(RegexLiteral) \
TYPE(SelectorName) \
TYPE(StartOfName) \
+ TYPE(StatementAttributeLikeMacro) \
TYPE(StatementMacro) \
TYPE(StructuredBindingLSquare) \
TYPE(TemplateCloser) \
diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp
index 8733834a079a..e9b096370dbb 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -52,6 +52,10 @@ FormatTokenLexer::FormatTokenLexer(
Macros.insert(
{&IdentTable.get(WhitespaceSensitiveMacro), TT_UntouchableMacroFunc});
}
+ for (const std::string &StatementAttributeLikeMacro :
+ Style.StatementAttributeLikeMacros)
+ Macros.insert({&IdentTable.get(StatementAttributeLikeMacro),
+ TT_StatementAttributeLikeMacro});
}
ArrayRef<FormatToken *> FormatTokenLexer::lex() {
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 1588f7d08184..34c291ecc492 100755
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1368,7 +1368,8 @@ class AnnotatingParser {
TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_JsFatArrow,
TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
- TT_UntouchableMacroFunc, TT_ConstraintJunctions))
+ TT_UntouchableMacroFunc, TT_ConstraintJunctions,
+ TT_StatementAttributeLikeMacro))
CurrentToken->setType(TT_Unknown);
CurrentToken->Role.reset();
CurrentToken->MatchingParen = nullptr;
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 9e47b49da3d0..8cacc5ad4cf4 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -655,6 +655,9 @@ void WhitespaceManager::alignConsecutiveDeclarations() {
return true;
if (C.Tok->isNot(TT_StartOfName))
return false;
+ if (C.Tok->Previous &&
+ C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
+ return false;
// Check if there is a subsequent name that starts the same declaration.
for (FormatToken *Next = C.Tok->Next; Next; Next = Next->Next) {
if (Next->is(tok::comment))
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 9fd41dbe7556..ae8bfc60f6d9 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -14731,6 +14731,11 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
std::vector<std::string>({"attr1", "attr2"}));
+ Style.StatementAttributeLikeMacros.clear();
+ CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
+ StatementAttributeLikeMacros,
+ std::vector<std::string>({"emit", "Q_EMIT"}));
+
Style.StatementMacros.clear();
CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
std::vector<std::string>{"QUNUSED"});
@@ -17803,6 +17808,36 @@ TEST_F(FormatTest, ConceptsAndRequires) {
"struct constant;",
Style);
}
+
+TEST_F(FormatTest, StatementAttributeLikeMacros) {
+ FormatStyle Style = getLLVMStyle();
+ StringRef Source = "void Foo::slot() {\n"
+ " unsigned char MyChar = 'x';\n"
+ " emit signal(MyChar);\n"
+ " Q_EMIT signal(MyChar);\n"
+ "}";
+
+ EXPECT_EQ(Source, format(Source, Style));
+
+ Style.AlignConsecutiveDeclarations = true;
+ EXPECT_EQ("void Foo::slot() {\n"
+ " unsigned char MyChar = 'x';\n"
+ " emit signal(MyChar);\n"
+ " Q_EMIT signal(MyChar);\n"
+ "}",
+ format(Source, Style));
+
+ Style.StatementAttributeLikeMacros.push_back("emit");
+ EXPECT_EQ(Source, format(Source, Style));
+
+ Style.StatementAttributeLikeMacros = {};
+ EXPECT_EQ("void Foo::slot() {\n"
+ " unsigned char MyChar = 'x';\n"
+ " emit signal(MyChar);\n"
+ " Q_EMIT signal(MyChar);\n"
+ "}",
+ format(Source, Style));
+}
} // namespace
} // namespace format
} // namespace clang
More information about the cfe-commits
mailing list