LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 38314 - Mis-counting of braces with redundant nested scopes
Summary: Mis-counting of braces with redundant nested scopes
Status: CONFIRMED
Alias: None
Product: clang
Classification: Unclassified
Component: Formatter (show other bugs)
Version: trunk
Hardware: PC All
: P enhancement
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-07-25 13:47 PDT by Drew Gross
Modified: 2021-11-25 01:06 PST (History)
5 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Drew Gross 2018-07-25 13:47:09 PDT
This code formats poorly:

namespace n {
void foo() {
    {
        {
            {
                statement();
                if (false) {
                }
            }
        }
        {

        }
    }
}
}

Formatter output (default settings, HEAD, reproduced here: https://zed0.co.uk/clang-format-configurator/):

namespace n {
void foo() {
  {
    {{statement();
    if (false) {
    }
  }
}
{}
}
}
}
Comment 1 MyDeveloperDay 2021-11-25 01:06:00 PST
I think more worrying is that this get the namespace matching incorrect unless I'm mistaken it thinks the namespace is the first scope level inside the foo()


namespace n {
void foo() {
  {                      <<<--- namespace } match to the {
    {{statement();
    if (false) {
    }
  }
}
{}
} // namespace n
}
}

I'm not saying I can explain what is going on here, other than clang-format must be confused.

Doing some experiments to try and break it down, we know this is good

namespace n {
void foo() {
  {
    statement();
    if (false) {
    }
  }
} // namespace n

And this

namespace n {
void foo() {
  {
    {
      statement();
      if (false) {
      }
    }
  }
} // namespace n

Even this is good

namespace n {
void foo() {
  {
    {
      statement();
      if (false) {
      }
    }
    {}
  }
} // namespace n


but this

namespace n {
void foo() {
  {
    {
      statement();
      if (false) {
      }
    }
  }
  {}
} // namespace n


would produce this..


namespace n {
void foo() {
  {{statement();
  if (false) {
  }
}
}
{}
} // namespace n

To reduce it more I think the if is irrelevant

namespace n {
void foo(){{statement();
}
}
{}
} // namespace n

This probably needs a greater looks