Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FixNamespaceComment not working as expected in .clang-format

I have a cpp code like :

#include<bits/stdc++.h>
using namespace std;

namespace a {
    const int b=1;
}

int main() {
    cout << "hello" << endl;
    return 0;
}

I tried the following configuration of .clang-format

Language:        Cpp 
BreakBeforeBraces: Custom
BraceWrapping:
  AfterClass:      false
  AfterStruct:     true
  BeforeCatch:     false
  BeforeElse:      false

FixNamespaceComments: true # add commend at end:
NamespaceIndentation: All #intend content of namespace

Expected output includes a comment at end of namespace closing bracket // namespace a. but it is not shown if there is only int a in namespace.

When I tried putting one more variable in namespace it worked fine.

I am using clang-format-6.0

like image 326
srbcheema1 Avatar asked Oct 20 '25 07:10

srbcheema1


2 Answers

It's been hardcoded in clang-format that namespace end comments aren't added to namespaces with only 1 line which seems quite arbitrary because there's not much difference between a namespace with 1 or 2 or 3 statements.

The offending code:

// The maximal number of unwrapped lines that a short namespace spans.
// Short namespaces don't need an end comment.
static const int kShortNamespaceMaxLines = 1;

https://github.com/llvm-mirror/clang/blob/release_70/lib/Format/NamespaceEndCommentsFixer.cpp

like image 106
goji Avatar answered Oct 22 '25 22:10

goji


Clang-Format will not add namespace end comment for "short" namespaces, i.e. namespaces that contain very few lines of code. Default is to not add the comment for namespaces of one line. The number of lines is configurable, see https://clang.llvm.org/docs/ClangFormatStyleOptions.html#shortnamespacelines. Setting the option to 0 should add namespace end comment to all namespaces.

like image 33
thilo Avatar answered Oct 22 '25 20:10

thilo