This is an archive of the discontinued LLVM Phabricator instance.

[libc] Warn on use of global constructors in `libc`
ClosedPublic

Authored by jhuber6 on Jul 19 2023, 9:02 AM.

Details

Summary

Clang supports the -Wglobal-constructors flag which will indicate if a
global constructor is being used. The current goal in libc is to make
the constructors constexpr to prevent this from happening with
straight construction. However, there are many other cases where we can
emit a constructor that this won't catch. This should give warning if
someone accidentally introduces a global constructor.

Diff Detail

Event Timeline

jhuber6 created this revision.Jul 19 2023, 9:02 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJul 19 2023, 9:02 AM
jhuber6 requested review of this revision.Jul 19 2023, 9:02 AM

I'm in favour. Definitely want this for the GPU targets even if the others want to stay warning-free

Does this currently give any warnings for an x86_64/linux fullbuild?

jhuber6 added a comment.EditedJul 19 2023, 11:27 AM

Does this currently give any warnings for an x86_64/linux fullbuild?

Yes,

/home/jhuber/Documents/llvm/llvm-project/libc/src/unistd/getopt.cpp:184:22: warning: declaration requires a global constructor [-Wglobal-constructors]     
  184 | static GetoptContext ctx{                                                                                   
      |                      ^  ~                                                                                 
  185 |     &impl::optarg, &impl::optind,                                                             
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                                     
  186 |     &impl::optopt, &optpos,                                                                        
      |     ~~~~~~~~~~~~~~~~~~~~~~~                                                                          
  187 |     impl::opterr,  reinterpret_cast<FILE *>(__llvm_libc::stderr)};                                          
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     

  /home/jhuber/Documents/llvm/llvm-project/libc/src/__support/File/linux/file.cpp:160:18: warning: declaration requires a global destructor [-Wglobal-constructors]
  160 | static LinuxFile StdIn(0, stdin_buffer, STDIN_BUFFER_SIZE, _IOFBF, false,                                   
      |                  ^                                                                                        
/home/jhuber/Documents/llvm/llvm-project/libc/src/__support/File/linux/file.cpp:166:18: warning: declaration requires a global destructor [-Wglobal-constructors]
  166 | static LinuxFile StdOut(1, stdout_buffer, STDOUT_BUFFER_SIZE, _IOLBF, false,                          
      |                  ^                                                                                 
/home/jhuber/Documents/llvm/llvm-project/libc/src/__support/File/linux/file.cpp:171:18: warning: declaration requires a global destructor [-Wglobal-constructors]
  171 | static LinuxFile StdErr(2, nullptr, STDERR_BUFFER_SIZE, _IONBF, false,                                        
      |                  ^                                                                                          
3 warnings generated.

I landed D155766 and D155762 so this should be okay to enable.

This revision is now accepted and ready to land.Jul 20 2023, 9:22 AM
This revision was landed with ongoing or failed builds.Jul 20 2023, 9:30 AM
This revision was automatically updated to reflect the committed changes.