Skip to content

[clang-format] Add the C language instead of treating it like C++ #128287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4786,17 +4786,20 @@ the configuration (without a prefix: ``Auto``).

.. note::

You can also specify the language (``Cpp`` or ``ObjC``) for ``.h`` files
by adding a ``// clang-format Language:`` line before the first
You can specify the language (``C``, ``Cpp``, or ``ObjC``) for ``.h``
files by adding a ``// clang-format Language:`` line before the first
non-comment (and non-empty) line, e.g. ``// clang-format Language: Cpp``.

Possible values:

* ``LK_None`` (in configuration: ``None``)
Do not use.

* ``LK_C`` (in configuration: ``C``)
Should be used for C.

* ``LK_Cpp`` (in configuration: ``Cpp``)
Should be used for C, C++.
Should be used for C++.

* ``LK_CSharp`` (in configuration: ``CSharp``)
Should be used for C#.
Expand Down
7 changes: 4 additions & 3 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,10 @@ clang-format
- Adds ``BreakBeforeTemplateCloser`` option.
- Adds ``BinPackLongBracedList`` option to override bin packing options in
long (20 item or more) braced list initializer lists.
- Allow specifying the language (C++ or Objective-C) for a ``.h`` file by adding
a special comment (e.g. ``// clang-format Language: ObjC``) near the top of
the file.
- Add the C language instead of treating it like C++.
- Allow specifying the language (C, C++, or Objective-C) for a ``.h`` file by
adding a special comment (e.g. ``// clang-format Language: ObjC``) near the
top of the file.

libclang
--------
Expand Down
14 changes: 10 additions & 4 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3318,7 +3318,9 @@ struct FormatStyle {
enum LanguageKind : int8_t {
/// Do not use.
LK_None,
/// Should be used for C, C++.
/// Should be used for C.
LK_C,
/// Should be used for C++.
LK_Cpp,
/// Should be used for C#.
LK_CSharp,
Expand All @@ -3343,7 +3345,9 @@ struct FormatStyle {
/// https://sci-hub.st/10.1109/IEEESTD.2018.8299595
LK_Verilog
};
bool isCpp() const { return Language == LK_Cpp || Language == LK_ObjC; }
bool isCpp() const {
return Language == LK_Cpp || Language == LK_C || Language == LK_ObjC;
}
bool isCSharp() const { return Language == LK_CSharp; }
bool isJson() const { return Language == LK_Json; }
bool isJavaScript() const { return Language == LK_JavaScript; }
Expand All @@ -3355,8 +3359,8 @@ struct FormatStyle {

/// The language that this format style targets.
/// \note
/// You can also specify the language (``Cpp`` or ``ObjC``) for ``.h`` files
/// by adding a ``// clang-format Language:`` line before the first
/// You can specify the language (``C``, ``Cpp``, or ``ObjC``) for ``.h``
/// files by adding a ``// clang-format Language:`` line before the first
/// non-comment (and non-empty) line, e.g. ``// clang-format Language: Cpp``.
/// \endnote
/// \version 3.5
Expand Down Expand Up @@ -5715,6 +5719,8 @@ FormatStyle::LanguageKind guessLanguage(StringRef FileName, StringRef Code);
// Returns a string representation of ``Language``.
inline StringRef getLanguageName(FormatStyle::LanguageKind Language) {
switch (Language) {
case FormatStyle::LK_C:
return "C";
case FormatStyle::LK_Cpp:
return "C++";
case FormatStyle::LK_CSharp:
Expand Down
12 changes: 11 additions & 1 deletion clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ template <> struct MappingTraits<FormatStyle::KeepEmptyLinesStyle> {

template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> {
static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) {
IO.enumCase(Value, "C", FormatStyle::LK_C);
IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp);
IO.enumCase(Value, "Java", FormatStyle::LK_Java);
IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript);
Expand Down Expand Up @@ -3957,7 +3958,12 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) {
LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;

LangOpts.LineComment = 1;
LangOpts.CXXOperatorNames = Style.isCpp();

const auto Language = Style.Language;
LangOpts.C17 = Language == FormatStyle::LK_C;
LangOpts.CXXOperatorNames =
Language == FormatStyle::LK_Cpp || Language == FormatStyle::LK_ObjC;

LangOpts.Bool = 1;
LangOpts.ObjC = 1;
LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
Expand All @@ -3982,6 +3988,8 @@ const char *StyleOptionHelpDescription =
" --style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";

static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
if (FileName.ends_with(".c"))
return FormatStyle::LK_C;
if (FileName.ends_with(".java"))
return FormatStyle::LK_Java;
if (FileName.ends_with_insensitive(".js") ||
Expand Down Expand Up @@ -4039,6 +4047,8 @@ static FormatStyle::LanguageKind getLanguageByComment(const Environment &Env) {
continue;

Text = Text.trim();
if (Text == "C")
return FormatStyle::LK_C;
if (Text == "Cpp")
return FormatStyle::LK_Cpp;
if (Text == "ObjC")
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Format/FormatToken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ static SmallVector<StringRef> CppNonKeywordTypes = {
bool FormatToken::isTypeName(const LangOptions &LangOpts) const {
if (is(TT_TypeName) || Tok.isSimpleTypeSpecifier(LangOpts))
return true;
const bool IsCpp = LangOpts.CXXOperatorNames;
return IsCpp && is(tok::identifier) &&
return (LangOpts.CXXOperatorNames || LangOpts.C17) && is(tok::identifier) &&
std::binary_search(CppNonKeywordTypes.begin(),
CppNonKeywordTypes.end(), TokenText);
}
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class AnnotatingParser {
: Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
IsCpp(Style.isCpp()), LangOpts(getFormattingLangOpts(Style)),
Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
assert(IsCpp == LangOpts.CXXOperatorNames);
assert(IsCpp == (LangOpts.CXXOperatorNames || LangOpts.C17));
Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
resetTokenMetadata();
}
Expand Down Expand Up @@ -3821,7 +3821,7 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
};

const auto *Next = Current.Next;
const bool IsCpp = LangOpts.CXXOperatorNames;
const bool IsCpp = LangOpts.CXXOperatorNames || LangOpts.C17;

// Find parentheses of parameter list.
if (Current.is(tok::kw_operator)) {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/TokenAnnotator.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class TokenAnnotator {
TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
: Style(Style), IsCpp(Style.isCpp()),
LangOpts(getFormattingLangOpts(Style)), Keywords(Keywords) {
assert(IsCpp == LangOpts.CXXOperatorNames);
assert(IsCpp == (LangOpts.CXXOperatorNames || LangOpts.C17));
}

/// Adapts the indent levels of comment lines to the indent of the
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ UnwrappedLineParser::UnwrappedLineParser(
: IG_Inited),
IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn),
Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable) {
assert(IsCpp == LangOpts.CXXOperatorNames);
assert(IsCpp == (LangOpts.CXXOperatorNames || LangOpts.C17));
}

void UnwrappedLineParser::reset() {
Expand Down
4 changes: 4 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24971,6 +24971,7 @@ TEST_F(FormatTest, StructuredBindings) {
}

TEST_F(FormatTest, FileAndCode) {
EXPECT_EQ(FormatStyle::LK_C, guessLanguage("foo.c", ""));
EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
Expand Down Expand Up @@ -25137,6 +25138,9 @@ TEST_F(FormatTest, GuessLanguageWithChildLines) {
}

TEST_F(FormatTest, GetLanguageByComment) {
EXPECT_EQ(FormatStyle::LK_C,
guessLanguage("foo.h", "// clang-format Language: C\n"
"int i;"));
EXPECT_EQ(FormatStyle::LK_Cpp,
guessLanguage("foo.h", "// clang-format Language: Cpp\n"
"int DoStuff(CGRect rect);"));
Expand Down