This is an archive of the discontinued LLVM Phabricator instance.

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

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

Details

Summary

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 (https://www.youtube.com/watch?v=omrY53kbVoA). 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

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) {
   Console.Print(name);
 }

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

clang/lib/Format/UnwrappedLineParser.cpp
3671

@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: https://github.com/carbon-language/carbon-lang/pull/1382

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.