Skip to content
Snippets Groups Projects

Fix clang compilation error

Merged Florian Uhlig requested to merge f.uhlig/cbmroot:fix_clang_error into master

The used structured bindings are only supported with C++20 and even then there is a bug in clang with some older version which crash during compilation. The fix doesn't capture the structured binding.

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Florian Uhlig changed milestone to %OCT23

    changed milestone to %OCT23

  • Florian Uhlig requested review from @fweig

    requested review from @fweig

  • assigned to @fweig

  • huh, structured bindings are a c++ 17 feature: https://en.cppreference.com/w/cpp/language/structured_binding

    We already use these in a number of places. Not sure why this one in particular is causing issues...

  • I'm kind of puzzled by this because structure bindings have been supported by clang since forever. Do you have the compiler version that crashed by any chance?

    I don't have an issue to merge this for a one-time workaround. But a whole bunch of things in algo are designed to be used with structured bindings. So this issue could just keep popping up again and again.

  • @fweig,

    when compiling the following code

    #include <tuple>
    
    std::tuple<int,int> x() {
            return {1,2};
    }
    
    void f() {
        auto [a, b] = x();
        auto l = [&]() {
            return a+b;
        };
    }
    
    int main() {
      f();
      return 1;
    }

    with clang 14 I get the following error

    /opt/homebrew/Cellar/llvm@14/14.0.6/bin/clang++ -std=c++17 test.cxx 
    
    test.cxx:10:16: error: reference to local binding 'a' declared in enclosing function 'f'
            return a+b;
                   ^
    test.cxx:8:11: note: 'a' declared here
        auto [a, b] = x();
              ^
    test.cxx:10:18: error: reference to local binding 'b' declared in enclosing function 'f'
            return a+b;
                     ^
    test.cxx:8:14: note: 'b' declared here
        auto [a, b] = x();
                 ^
    2 errors generated.

    when compiling with clang 16 which supports the structured bindings I get the following warning

    /opt/homebrew/Cellar/llvm@16/16.0.6/bin/clang++ -std=c++17 test.cxx 
    test.cxx:10:16: warning: captured structured bindings are a C++20 extension [-Wc++20-extensions]
            return a+b;
                   ^
    test.cxx:8:11: note: 'a' declared here
        auto [a, b] = x();
              ^
    test.cxx:10:18: warning: captured structured bindings are a C++20 extension [-Wc++20-extensions]
            return a+b;
                     ^
    test.cxx:8:14: note: 'b' declared here
        auto [a, b] = x();
                 ^
    2 warnings generated.

    I have no idea why clang and cppreference disagree and I also have no clue why the compilation crashes for me.

    I am also fine if the code isn't changed and I patch my version only locally.

  • Ok, thanks. I can reproduce the errors in godbolt and gcc accepts this without error or warning. But I'm still confused: These errors are related to lambda-capture with structured bindings. We don't use that at all in the patched code?

  • @fweig,

    as I said I don't understand the issue completely. The original error message was

    /tmp/cbmroot/algo/unpack/Unpack.cxx:198:28: error: reference to local binding 'numMs' declared in enclosing function 'cbm::algo::Unpack::ParallelMsLoop<CbmStsDigi, cbm::algo::sts::Unpack, cbm::algo::sts::UnpackMonitorData>'
        for (size_t i = 0; i < numMs; i++) {
                               ^
    /tmp/cbmroot/algo/unpack/Unpack.cxx:35:9: note: in instantiation of function template specialization 'cbm::algo::Unpack::ParallelMsLoop<CbmStsDigi, cbm::algo::sts::Unpack, cbm::algo::sts::UnpackMonitorData>' requested here
            ParallelMsLoop(Subsystem::STS, monitor, digiTs.fSts, monitor.fSts, *timeslice, fAlgoSts, 0x20);
            ^
    /tmp/cbmroot/algo/unpack/Unpack.cxx:187:11: note: 'numMs' declared here
        auto [numMs, sizeBytes] =
              ^
    /tmp/cbmroot/algo/unpack/Unpack.cxx:216:28: error: reference to local binding 'numMs' declared in enclosing function 'cbm::algo::Unpack::ParallelMsLoop<CbmStsDigi, cbm::algo::sts::Unpack, cbm::algo::sts::UnpackMonitorData>'
        for (size_t i = 0; i < numMs; i++) {
                               ^
    /tmp/cbmroot/algo/unpack/Unpack.cxx:187:11: note: 'numMs' declared here
        auto [numMs, sizeBytes] =
              ^
    /tmp/cbmroot/algo/unpack/Unpack.cxx:198:28: error: reference to local binding 'numMs' declared in enclosing function 'cbm::algo::Unpack::ParallelMsLoop<CbmTofDigi, cbm::algo::tof::Unpack, cbm::algo::tof::UnpackMonitorData>'
        for (size_t i = 0; i < numMs; i++) {
                               ^
    /tmp/cbmroot/algo/unpack/Unpack.cxx:40:9: note: in instantiation of function template specialization 'cbm::algo::Unpack::ParallelMsLoop<CbmTofDigi, cbm::algo::tof::Unpack, cbm::algo::tof::UnpackMonitorData>' requested here
            ParallelMsLoop(Subsystem::TOF, monitor, digiTs.fTof, monitor.fTof, *timeslice, fAlgoTof, 0x00);
            ^
    /tmp/cbmroot/algo/unpack/Unpack.cxx:187:11: note: 'numMs' declared here
        auto [numMs, sizeBytes] =
              ^
    /tmp/cbmroot/algo/unpack/Unpack.cxx:216:28: error: reference to local binding 'numMs' declared in enclosing function 'cbm::algo::Unpack::ParallelMsLoop<CbmTofDigi, cbm::algo::tof::Unpack, cbm::algo::tof::UnpackMonitorData>'
        for (size_t i = 0; i < numMs; i++) {
                               ^
    /tmp/cbmroot/algo/unpack/Unpack.cxx:187:11: note: 'numMs' declared here
        auto [numMs, sizeBytes] =
              ^

    numMs is already used before without an error

        std::vector<std::vector<Digi>> msDigis(numMs);  // unpacked digis per microslice
        std::vector<Monitor> monitor(numMs);            // unpacking monitoring data per microslice

    Whatever is different when using numMs in the loop.

    The error is only raised when using numMs in the loop.

  • Thanks! Looks like a weird issue with the OpenMP section... Sorry for dragging this on. I wanted to understand the issue, because this seems like something we might encounter again.

  • Felix Weiglhofer approved this merge request

    approved this merge request

  • Edited by Felix Weiglhofer
  • @fweig,

    thanks for your investigation.

  • Florian Uhlig added 5 commits

    added 5 commits

    Compare with previous version

  • Felix Weiglhofer enabled an automatic merge when the pipeline for 69e77dce succeeds

    enabled an automatic merge when the pipeline for 69e77dce succeeds

  • Dear @d.smith, @fweig,

    you have been identified as code owner of at least one file which was changed with this merge request.

    Please check the changes and approve them or request changes.

  • Felix Weiglhofer mentioned in merge request !1494 (merged)

    mentioned in merge request !1494 (merged)

Please register or sign in to reply
Loading