[PATCH] D96725: [clang-tidy] Fix modernize-use-using in extern C code

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 16 04:42:23 PST 2021


aaron.ballman added inline comments.


================
Comment at: clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp:305
 };
+
+extern "C" {
----------------
steveire wrote:
> njames93 wrote:
> > steveire wrote:
> > > Can you add tests for typedefs in other scopes like
> > > 
> > > ```
> > > 
> > > extern "C" {
> > > typedef int CType;
> > > 
> > > struct CAnother {
> > > };
> > > 
> > > typedef struct {
> > >   int b;
> > >   typedef struct CAnother AStruct;
> > > } CStruct;
> > > 
> > > void foo()
> > > {
> > >   typedef struct CAnother AStruct;
> > > }
> > > }
> > > 
> > > ```
> > I'm not sure those tests add any value. For the struct, you can't have a typedef in a struct in c. The function typedef is valid in c, but I still can't see a reason to use extern C when defining a function in c++
> Fair enough - Clang accepts the typedef in the struct with a warning, but it doesn't get transformed by the check anyway. The one in the function doesn't get transformed either, and if functions are so unlikely to appear in extern C, that it doesn't need to be covered, that's fine with me.
> I still can't see a reason to use extern C when defining a function in c++

There are a few reasons.

1) You're defining an extern linkage function with C language linkage and not providing the declaration or you're being consistent between the declaration and the definition.

2) You have an `extern "C"` block that contains function definitions.

3) Pedantically, if the function declaration is `extern "C"`, the function definition must be as well (http://eel.is/c++draft/dcl.link#1.sentence-5, http://eel.is/c++draft/dcl.link#6), however, many compilers ignore these requirements because otherwise the STL has issues.

So it definitely does happen in the wild. As some examples:

https://codesearch.isocpp.org/actcd19/main/b/bali-phy/bali-phy_3.4+dfsg-1/src/builtins/SModel.cc
https://codesearch.isocpp.org/actcd19/main/o/openjdk-11/openjdk-11_11.0.3+1-1/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach015/attach015Agent00.c
https://codesearch.isocpp.org/actcd19/main/m/mp3fs/mp3fs_0.91-1/src/transcode.cc
https://codesearch.isocpp.org/actcd19/main/t/theano/theano_0.8.2-6/theano/sandbox/cuda/cnmem.cpp


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D96725/new/

https://reviews.llvm.org/D96725



More information about the cfe-commits mailing list