[clang] [Tooling/Inclusion] Avoid narrowing conversions in macro expansion (PR #72664)

via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 17 07:26:21 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Krzysztof Parzyszek (kparzysz)

<details>
<summary>Changes</summary>

```
clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp:144:65: warning: narrowing conversion of ‘llvm::StringRef(((const char*)"std::experimental::filesystem::")).llvm::StringRef::size()’ from ‘size_t’ {aka ‘long un signed int’} to ‘unsigned int’ [-Wnarrowing]
  144 | #define SYMBOL(Name, NS, Header) {#NS #Name, StringRef(#NS).size(), #Header},
      |                                              ~~~~~~~~~~~~~~~~~~~^~
clang/lib/Tooling/Inclusions/Stdlib/StdTsSymbolMap.inc:51:1: note: in expansion of macro ‘SYMBOL’
   51 | SYMBOL(temp_directory_path, std::experimental::filesystem::, <experimental/filesystem>)
      | ^~~~~~
```

---
Full diff: https://github.com/llvm/llvm-project/pull/72664.diff


1 Files Affected:

- (modified) clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp (+3-1) 


``````````diff
diff --git a/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp b/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp
index 03f61d33e1f26e7..adf1b230ff03181 100644
--- a/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp
+++ b/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp
@@ -141,7 +141,9 @@ static int initialize(Lang Language) {
     unsigned NSLen;
     const char *HeaderName;
   };
-#define SYMBOL(Name, NS, Header) {#NS #Name, StringRef(#NS).size(), #Header},
+#define SYMBOL(Name, NS, Header)                                               \
+  {#NS #Name, static_cast<decltype(Symbol::NSLen)>(StringRef(#NS).size()),     \
+   #Header},
   switch (Language) {
   case Lang::C: {
     static constexpr Symbol CSymbols[] = {

``````````

</details>


https://github.com/llvm/llvm-project/pull/72664


More information about the cfe-commits mailing list