<div dir="ltr"><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div>Hi all,<br><br></div>First post.<br><br></div>I've found clang-format to be a very powerful and useful tool and I'd like to start extending it to support some other formatting styles that I have to work with. The two changes I'd like to start with are to namespaces and function parameters.<br></div>For namespaces I'd like create a configuration parameter that would allow nested namespaces all on one line.<br></div><br>Rather than<br></div><br>namespace a {<br></div>namespace b {<br></div>namespace c{<br>}<br>}<br></div>};<br><br></div>Support<br><br></div>namespace a { namespace b { namespace c {<br>}}};<br><br></div>and for functions rather than<br><br></div>int foo(int param1,<br></div>          int param2)<br>{<br>}<br><br></div>Support<br><br></div>int foo<br>(<br></div>    int param1,<br></div>    int pararm2<br>)<br>{<br>}<br><div><div><div><div><div><div>       <br><br></div><div>I've started to poke around in the clang lib/Format code but I'm finding it difficult to figure out where the best place to make the changes is and there isn't much documentation on the clang-format design/code I can find.<br></div><div>I think the rough idea in clang-format is to use the UnwrappedLineParser to separate the code into the appropriate lines, then using the UnwrappedLineFormatter annotate the tokens, merge lines where possible and delete unnecessary lines then finally dump it all.<br><br></div><div>Taking my namespace change for example I've made modifications to the UnwrappedLineParser::parseBlock so it won't always add a newline from parseNamespace<br><br>-----------------------------------------------------<br>@@ -399,7 +399,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {<br> }<br> <br> void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,<br>-                                     bool MunchSemi) {<br>+                                     bool MunchSemi, bool AddLine) {<br>   assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&<br>          "'{' or macro block token expected");<br>   const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);<br>@@ -410,7 +410,7 @@ void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,<br>   if (MacroBlock && FormatTok->is(tok::l_paren))<br>     parseParens();<br> <br>-  addUnwrappedLine();<br>+  if (AddLine) addUnwrappedLine();<br> <br>   ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,<br>                                           MustBeDeclaration);<br>---------------------------------<br><br></div><div>and this works somewhat but the namespaces are now missing whitespaces and you get<br></div><div><br>namespace a{namespace b{namespace c{<br>}<br>}<br>}<br></div><div><br></div><div>So instead should I write a line merger for namespaces? Or should I find a way to update the insert whitespace before/after flag?<br><br></div><div>I guess I'm just looking for some guidance on how to best approach this kind of work or to be pointed in the direction of more doco.<br><br></div><div>Thanks in advance,<br><br></div><div>Andrew<br></div></div></div></div></div></div></div>