[PATCH] D35485: [clang-format] Fix comment levels between '}' and PPDirective

Krasimir Georgiev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 19 08:53:58 PDT 2017


krasimir added inline comments.


================
Comment at: lib/Format/UnwrappedLineParser.cpp:489
 
-  nextToken(); // Munch the closing brace.
+  nextToken(InitialLevel); // Munch the closing brace.
 
----------------
djasper wrote:
> krasimir wrote:
> > djasper wrote:
> > > krasimir wrote:
> > > > djasper wrote:
> > > > > What happens if you instead change the Line->Level = InitialLevel; statement from below to before this line? That seems like the more intuitively correct fix.
> > > > This doesn't work since comments before the right brace haven't been emitted yet and would get the wrong level.
> > > So that means this seems to be the interesting case:
> > > 
> > >   void f() {
> > >     DoSomething();
> > >     // This was a fun function.
> > >   }
> > >   // Cool macro:
> > >   #define A a
> > > 
> > > Now, both comments are basically read when we are reading the "}", but they should have different indentation levels. I have another suggestion, see below.
> > Here is another breaking test in case we change `Line->Level = InitialLevel` to above this line:
> > ```
> > switch (x) {
> > default: {
> >   // Do nothing.
> > }
> > }
> > ```
> > gets reformatted as:
> > ```
> > switch (x) {
> > default: {
> > // Do nothing.
> > }
> > }
> > ```
> I think we can fix all of these cases by doing:
> 
>   flushComments(isOnNewLine(*FormatTok));
>   Line->Level = InitialLevel;
>   nextToken(); // Munch the closing brace.
> 
> (so add the first two lines here, remove Line->Level = InitialLevel; below.
Cool! Thanks!


https://reviews.llvm.org/D35485





More information about the cfe-commits mailing list