llvm-project/clang/test/Modules/diag-undefined-template.cpp
Haojian Wu 434ac4612f
Improve the -Wundefined-func-template diagnostic note for invisible template functions (#129031)
See discussion in https://github.com/llvm/llvm-project/issues/125071.

Makes the note clearer for the unreachable case:

Before:
```
./hoge.h:5:12: warning: instantiation of function 'x<int>' required here, but no definition is available [-Wundefined-func-template]
    5 | void f() { x<int>(); }
      |            ^
./shared_ptr2.h:4:6: note: forward declaration of template entity is here
    4 | void x() { T t; (void)t; }
      |      ^
./hoge.h:5:12: note: add an explicit instantiation declaration to suppress this warning if 'x<int>' is explicitly instantiated in another translation unit
    5 | void f() { x<int>(); }
      |    
```

After:

```
./hoge.h:5:12: warning: instantiation of function 'x<int>' required here, but no definition is available [-Wundefined-func-template]
    5 | void f() { x<int>(); }
      |            ^
./shared_ptr2.h:4:6: note: declaration of template entity is unreachable here
    4 | void x() { T t; (void)t; }
      |      ^
1 warning generated.
```
2025-03-19 10:51:45 +01:00

40 lines
797 B
C++

// RUN: rm -rf %t
// RUN: split-file %s %t
// RUN: %clang_cc1 -std=c++20 -fmodules -fmodules-cache-path=%t -I%t \
// RUN: -Wundefined-func-template \
// RUN: -fimplicit-module-maps %t/main.cpp 2>&1 | grep "unreachable declaration of template entity is here"
// Note that the diagnostics are triggered when building the 'hoge' module, which is imported from the main file.
// The "-verify" flag doesn't work in this case. Instead, we grep the expected text to verify the test.
//--- shared_ptr2.h
#pragma once
template<class T>
void x() { }
//--- hoge.h
#pragma once
#include "shared_ptr2.h"
inline void f() {
x<int>();
}
//--- module.modulemap
module hoge {
header "hoge.h"
}
module shared_ptr2 {
header "shared_ptr2.h"
}
//--- main.cpp
#include "hoge.h"
int main() {
f();
}