Page MenuHomePhabricator

[clang-format] Add Basic Carbon Support/Infrastructure to clang-format
Needs ReviewPublic

Authored by MyDeveloperDay on Sep 26 2022, 9:32 AM.



Its great when clang-format is the ubiquitous tool for all formatting regardless of the language. clang-formats existing integration into common editors/IDEs means we can build on the clang-format legacy to help contribute in a small way to the future of Carbon, simply by helping providing native Carbon support.

Following the excellent talk by @chandlerc et al at CppNorth ( On seeing the suggested syntax it was clear that clang-format would face some new challenges. (which might be exciting to work on)

This review is to add the basic infrastructure for Carbon as a potential clang-format language, to begin building that infrastructure and to fix a couple of Annotator issues which caused problems even with the simple language examples, namely "spaces around trailing return arrows and an additional space between type and colon in variable declarations"

I'm mainly submitting this to gauge interest from the team, and to see if others feel this might be something that we are interested in pursuing. (its also a good way to learn about Carbon.)

Diff Detail

Unit TestsFailed

240 msx64 debian > LLVM.Transforms/LoopVectorize/AArch64::masked-op-cost.ll
Script: -- : 'RUN: at line 2'; /var/lib/buildkite-agent/builds/llvm-project/build/bin/opt -loop-vectorize -force-vector-interleave=1 -S -debug-only=loop-vectorize < /var/lib/buildkite-agent/builds/llvm-project/llvm/test/Transforms/LoopVectorize/AArch64/masked-op-cost.ll 2>/var/lib/buildkite-agent/builds/llvm-project/build/test/Transforms/LoopVectorize/AArch64/Output/masked-op-cost.ll.tmp | /var/lib/buildkite-agent/builds/llvm-project/build/bin/FileCheck /var/lib/buildkite-agent/builds/llvm-project/llvm/test/Transforms/LoopVectorize/AArch64/masked-op-cost.ll

Event Timeline

MyDeveloperDay created this revision.Sep 26 2022, 9:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 26 2022, 9:32 AM
MyDeveloperDay requested review of this revision.Sep 26 2022, 9:32 AM
MyDeveloperDay updated this revision to Diff 462959.EditedSep 26 2022, 10:14 AM

Ensure the colon stick with the variable declaration in a loop

for (var name: String in names) {

Treat a class like a C# or Javascript class there is no semi after the brace };

class Sum {
  var a: i32;
  fn Add[me:Self](var num: i32) -> i32 {
    var total: i32 = me.a + num;
    return total;
fn Main() -> i32 {
  var p1: Sum = {.a = 5};
  var total: i32 = p1.Add(5);
  Print("Total sum {0}", total);
  return 0;

Fix pointer function arguments
Add release notes
Fix more TT_TrailingReturnArrow cases

Remove .clang-format change

MyDeveloperDay retitled this revision from [clang-format] Add Really Basic Carbon Support/Infrastructure to [clang-format] Add Basic Carbon Support/Infrastructure to clang-format.

Improve the handling of generics.
Ensure match is formatted like switch.
Move closer to Google style.
Add some pointer support for return types
Add additional integration


@HazardyKnusperkeks thank you, any thoughts from you or others on if you feel its ok for me to continue?

I'm not against extending clang-format to support yet another language like Carbon, but we should probably wait until it has gained widespread usage?

rymiel added a subscriber: rymiel.Oct 2 2022, 1:47 AM

I think the above is especially relevant as Carbon may yet change everything about its syntax; for example, in this patch, me has been made a keyword, but that may change in the future:

I'm not actually really considering the new "keywords" much at the moment (less match), some of this is really about formatting the Carbon fundamentals (SpaceBetween etc..) to a Google style.