[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread Vitaly Buka via cfe-commits

vitalybuka wrote:

Thank you!
Sorry, I should search for reverts before complaining.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread Aaron Ballman via cfe-commits

AaronBallman wrote:

> @budimirarandjelovicsyrmia Any updates? Should we revert the patch?

I already reverted in 9d1017204a253782a82d1b7d16c59eea3f811a11 so the bots have 
been slowly getting back to green

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread Vitaly Buka via cfe-commits

vitalybuka wrote:

@budimirarandjelovicsyrmia Any updates? Should we revert the patch?

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder 
`sanitizer-aarch64-linux-bootstrap-asan` running on `sanitizer-buildbot8` while 
building `clang` at step 2 "annotate".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/24/builds/449

Here is the relevant piece of the build log for the reference:
```
Step 2 (annotate) failure: 'python 
../sanitizer_buildbot/sanitizers/zorg/buildbot/builders/sanitizers/buildbot_selector.py'
 (failure)
...
llvm-lit: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:508:
 note: using lld-link: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm_build_asan/bin/lld-link
llvm-lit: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:508:
 note: using ld64.lld: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm_build_asan/bin/ld64.lld
llvm-lit: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:508:
 note: using wasm-ld: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm_build_asan/bin/wasm-ld
llvm-lit: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:508:
 note: using ld.lld: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm_build_asan/bin/ld.lld
llvm-lit: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:508:
 note: using lld-link: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm_build_asan/bin/lld-link
llvm-lit: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:508:
 note: using ld64.lld: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm_build_asan/bin/ld64.lld
llvm-lit: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:508:
 note: using wasm-ld: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm_build_asan/bin/wasm-ld
llvm-lit: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/main.py:72:
 note: The test suite configuration requested an individual test timeout of 0 
seconds but a timeout of 900 seconds was requested on the command line. Forcing 
timeout to be 900 seconds.
-- Testing: 81063 of 81064 tests, 48 workers --
Testing:  0.. 10..
FAIL: Clang :: Sema/attr-format-missing.c (16169 of 81063)
 TEST 'Clang :: Sema/attr-format-missing.c' FAILED 

Exit Code: 1

Command Output (stderr):
--
RUN: at line 1: 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm_build_asan/bin/clang -cc1 
-internal-isystem 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm_build_asan/lib/clang/19/include
 -nostdsysteminc -fsyntax-only -verify=expected,c_diagnostics 
-Wmissing-format-attribute 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/clang/test/Sema/attr-format-missing.c
+ /b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm_build_asan/bin/clang 
-cc1 -internal-isystem 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm_build_asan/lib/clang/19/include
 -nostdsysteminc -fsyntax-only -verify=expected,c_diagnostics 
-Wmissing-format-attribute 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/clang/test/Sema/attr-format-missing.c
error: 'c_diagnostics-error' diagnostics seen but not expected: 
  File 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/clang/test/Sema/attr-format-missing.c
 Line 87: subscripted value is not an array, pointer, or vector
  File 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/clang/test/Sema/attr-format-missing.c
 Line 95: subscripted value is not an array, pointer, or vector
  File 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/clang/test/Sema/attr-format-missing.c
 Line 96: subscripted value is not an array, pointer, or vector
  File 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/clang/test/Sema/attr-format-missing.c
 Line 391: passing 'char *' to parameter of incompatible type 
'__builtin_va_list'
error: 'c_diagnostics-warning' diagnostics expected but not seen: 
  File 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/clang/test/Sema/attr-format-missing.c
 Line 83 (directive at 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/clang/test/Sema/attr-format-missing.c:87):
 diagnostic behavior may be improved by adding the 'scanf' format attribute to 
the declaration of 'f7'
  File 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/clang/test/Sema/attr-format-missing.c
 Line 111 (directive at 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/clang/test/Sema/attr-format-missing.c:113):
 incompatible pointer types passing 'const wchar_t *' (aka 'const int *') to 
parameter of type 'const char *'
  File 
/b/sanitizer-aarch64-linux-bootstrap-asan/build/llvm-project/clang/test/Sema/attr-format-missing.c
 Line 141 (directive at 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread Aaron Ballman via cfe-commits

AaronBallman wrote:

There are multiple distinct issues that were caught:

build failure -- https://lab.llvm.org/buildbot/#/builders/72/builds/1123
sanitizer failure -- https://lab.llvm.org/buildbot/#/builders/164/builds/920
different failure from above -- 
https://lab.llvm.org/buildbot/#/builders/193/builds/896
etc

You should go through each of the failures individually to ensure you've 
addressed all the concerns. I would not remove test coverage, but instead 
repair the tests.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread Budimir Aranđelović via cfe-commits

budimirarandjelovicsyrmia wrote:

Problem is in function f41 where diagnosing depends on architecture and 
operating system.
```
void f41(char *out, ... /* args */) // #f41
{
va_list args;
char *ch;
vscanf("%s", ch);
vprintf(out, args);
}
```

As this is unusual situation, I suppose to remove this function from test.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder 
`sanitizer-x86_64-linux-bootstrap-msan` running on `sanitizer-buildbot5` while 
building `clang` at step 2 "annotate".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/164/builds/920

Here is the relevant piece of the build log for the reference:
```
Step 2 (annotate) failure: 'python 
../sanitizer_buildbot/sanitizers/zorg/buildbot/builders/sanitizers/buildbot_selector.py'
 (failure)
...
llvm-lit: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:508:
 note: using lld-link: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm_build_msan/bin/lld-link
llvm-lit: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:508:
 note: using ld64.lld: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm_build_msan/bin/ld64.lld
llvm-lit: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:508:
 note: using wasm-ld: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm_build_msan/bin/wasm-ld
llvm-lit: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:508:
 note: using ld.lld: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm_build_msan/bin/ld.lld
llvm-lit: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:508:
 note: using lld-link: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm_build_msan/bin/lld-link
llvm-lit: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:508:
 note: using ld64.lld: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm_build_msan/bin/ld64.lld
llvm-lit: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:508:
 note: using wasm-ld: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm_build_msan/bin/wasm-ld
llvm-lit: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/llvm/utils/lit/lit/main.py:72:
 note: The test suite configuration requested an individual test timeout of 0 
seconds but a timeout of 900 seconds was requested on the command line. Forcing 
timeout to be 900 seconds.
-- Testing: 83950 tests, 80 workers --
Testing: 
FAIL: Clang :: ARCMT/GC-check.m (131 of 83950)
 TEST 'Clang :: ARCMT/GC-check.m' FAILED 

Exit Code: 1

Command Output (stderr):
--
RUN: at line 1: 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm_build_msan/bin/clang -cc1 
-internal-isystem 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm_build_msan/lib/clang/19/include
 -nostdsysteminc -arcmt-action=check -verify -triple x86_64-apple-darwin10 
-fobjc-gc-only 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/clang/test/ARCMT/GC-check.m
+ /b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm_build_msan/bin/clang -cc1 
-internal-isystem 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm_build_msan/lib/clang/19/include
 -nostdsysteminc -arcmt-action=check -verify -triple x86_64-apple-darwin10 
-fobjc-gc-only 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/clang/test/ARCMT/GC-check.m
==2197391==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x68649bde in clang::Sema::GetMissingFormatAttributes(clang::Stmt*, 
clang::FunctionDecl const*) 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/clang/lib/Sema/SemaDeclAttr.cpp:5455:11
#1 0x68646747 in 
clang::Sema::DiagnoseMissingFormatAttributes(clang::Stmt*, clang::FunctionDecl 
const*) 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/clang/lib/Sema/SemaDeclAttr.cpp:5336:7
#2 0x6846a274 in clang::Sema::ActOnFinishFunctionBody(clang::Decl*, 
clang::Stmt*, bool) 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/clang/lib/Sema/SemaDecl.cpp:15937:7
#3 0x67dca9ad in 
clang::Parser::ParseFunctionStatementBody(clang::Decl*, 
clang::Parser::ParseScope&) 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/clang/lib/Parse/ParseStmt.cpp:2536:18
#4 0x67be11a2 in 
clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, 
clang::Parser::ParsedTemplateInfo const&, clang::Parser::LateParsedAttrList*) 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/clang/lib/Parse/Parser.cpp:1525:10
#5 0x67d2e25f in clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, 
clang::DeclaratorContext, clang::ParsedAttributes&, 
clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, 
clang::Parser::ForRangeInit*) 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/clang/include/clang/Parse/Parser.h
#6 0x67bdccd3 in 
clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, 
clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) 
/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/clang/lib/Parse/Parser.cpp:1249:10
#7 0x67bdb525 in 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder `llvm-clang-win-x-aarch64` 
running on `as-builder-2` while building `clang` at step 9 "test-check-clang".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/193/builds/896

Here is the relevant piece of the build log for the reference:
```
Step 9 (test-check-clang) failure: Test just built components for check-clang 
completed (failure)
 TEST 'Clang :: Sema/attr-format-missing.c' FAILED 

Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
c:\buildbot\as-builder-2\x-aarch64\build\bin\clang.exe -cc1 -internal-isystem 
C:\buildbot\as-builder-2\x-aarch64\build\lib\clang\19\include -nostdsysteminc 
-fsyntax-only -verify=expected,c_diagnostics -Wmissing-format-attribute 
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c
# executed command: 'c:\buildbot\as-builder-2\x-aarch64\build\bin\clang.exe' 
-cc1 -internal-isystem 
'C:\buildbot\as-builder-2\x-aarch64\build\lib\clang\19\include' -nostdsysteminc 
-fsyntax-only -verify=expected,c_diagnostics -Wmissing-format-attribute 
'C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c'
# .---command stderr
# | error: 'c_diagnostics-error' diagnostics seen but not expected: 
# |   File 
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c
 Line 87: subscripted value is not an array, pointer, or vector
# |   File 
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c
 Line 95: subscripted value is not an array, pointer, or vector
# |   File 
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c
 Line 96: subscripted value is not an array, pointer, or vector
# |   File 
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c
 Line 391: passing 'char *' to parameter of incompatible type 
'__builtin_va_list'
# | error: 'c_diagnostics-warning' diagnostics expected but not seen: 
# |   File 
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c
 Line 83 (directive at 
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c:87):
 diagnostic behavior may be improved by adding the 'scanf' format attribute to 
the declaration of 'f7'
# |   File 
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c
 Line 111 (directive at 
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c:113):
 incompatible pointer types passing 'const wchar_t *' (aka 'const int *') to 
parameter of type 'const char *'
# |   File 
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c
 Line 141 (directive at 
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c:143):
 incompatible pointer types passing 'const wchar_t *' (aka 'const int *') to 
parameter of type 'const char *'
# | error: 'c_diagnostics-warning' diagnostics seen but not expected: 
# |   File 
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c
 Line 111: incompatible pointer types passing 'const wchar_t *' (aka 'const 
unsigned int *') to parameter of type 'const char *'
# |   File 
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c
 Line 141: incompatible pointer types passing 'const wchar_t *' (aka 'const 
unsigned int *') to parameter of type 'const char *'
# |   File 
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c
 Line 387: diagnostic behavior may be improved by adding the 'printf' format 
attribute to the declaration of 'f41'
# | error: 'c_diagnostics-note' diagnostics seen but not expected: 
# |   File 
C:\buildbot\as-builder-2\x-aarch64\llvm-project\clang\test\Sema\attr-format-missing.c
 Line 30: passing argument to parameter here
# | 11 errors generated.
# `-
# error: command failed with exit status: 1

--




```

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder `llvm-clang-x86_64-darwin` 
running on `doug-worker-3` while building `clang` at step 6 
"test-build-unified-tree-check-all".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/23/builds/925

Here is the relevant piece of the build log for the reference:
```
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
 TEST 'Clang :: Sema/attr-format-missing.c' FAILED 

Exit Code: 1

Command Output (stderr):
--
RUN: at line 1: /Volumes/RAMDisk/buildbot-root/x86_64-darwin/build/bin/clang 
-cc1 -internal-isystem 
/Volumes/RAMDisk/buildbot-root/x86_64-darwin/build/lib/clang/19/include 
-nostdsysteminc -fsyntax-only -verify=expected,c_diagnostics 
-Wmissing-format-attribute 
/Volumes/RAMDisk/buildbot-root/x86_64-darwin/llvm-project/clang/test/Sema/attr-format-missing.c
+ /Volumes/RAMDisk/buildbot-root/x86_64-darwin/build/bin/clang -cc1 
-internal-isystem 
/Volumes/RAMDisk/buildbot-root/x86_64-darwin/build/lib/clang/19/include 
-nostdsysteminc -fsyntax-only -verify=expected,c_diagnostics 
-Wmissing-format-attribute 
/Volumes/RAMDisk/buildbot-root/x86_64-darwin/llvm-project/clang/test/Sema/attr-format-missing.c
error: 'c_diagnostics-warning' diagnostics seen but not expected: 
  File 
/Volumes/RAMDisk/buildbot-root/x86_64-darwin/llvm-project/clang/test/Sema/attr-format-missing.c
 Line 391: incompatible pointer types passing 'char *' to parameter of type 
'struct __va_list_tag *'
error: 'c_diagnostics-note' diagnostics seen but not expected: 
  File 
/Volumes/RAMDisk/buildbot-root/x86_64-darwin/llvm-project/clang/test/Sema/attr-format-missing.c
 Line 30: passing argument to parameter here
2 errors generated.

--




```

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder `clang-solaris11-sparcv9` 
running on `solaris11-sparcv9` while building `clang` at step 5 "ninja check 1".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/13/builds/686

Here is the relevant piece of the build log for the reference:
```
Step 5 (ninja check 1) failure: stage 1 checked (failure)
 TEST 'Clang :: Sema/attr-format-missing.c' FAILED 

Exit Code: 1

Command Output (stderr):
--
RUN: at line 1: 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/bin/clang
 -cc1 -internal-isystem 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/lib/clang/19/include
 -nostdsysteminc -fsyntax-only -verify=expected,c_diagnostics 
-Wmissing-format-attribute 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/llvm/clang/test/Sema/attr-format-missing.c
+ 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/bin/clang
 -cc1 -internal-isystem 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/lib/clang/19/include
 -nostdsysteminc -fsyntax-only -verify=expected,c_diagnostics 
-Wmissing-format-attribute 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/llvm/clang/test/Sema/attr-format-missing.c
RUN: at line 2: 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/bin/clang
 -cc1 -internal-isystem 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/lib/clang/19/include
 -nostdsysteminc -fsyntax-only -Wmissing-format-attribute 
-fdiagnostics-parseable-fixits 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/llvm/clang/test/Sema/attr-format-missing.c
 2>&1 | 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/bin/FileCheck
 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/llvm/clang/test/Sema/attr-format-missing.c
 --check-prefixes=CHECK,C-CHECK
+ 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/bin/clang
 -cc1 -internal-isystem 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/lib/clang/19/include
 -nostdsysteminc -fsyntax-only -Wmissing-format-attribute 
-fdiagnostics-parseable-fixits 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/llvm/clang/test/Sema/attr-format-missing.c
+ 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/bin/FileCheck
 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/llvm/clang/test/Sema/attr-format-missing.c
 --check-prefixes=CHECK,C-CHECK
RUN: at line 3: 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/bin/clang
 -cc1 -internal-isystem 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/lib/clang/19/include
 -nostdsysteminc -fsyntax-only -x c++ -verify=expected,cpp_diagnostics 
-Wmissing-format-attribute 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/llvm/clang/test/Sema/attr-format-missing.c
+ 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/bin/clang
 -cc1 -internal-isystem 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/stage1/lib/clang/19/include
 -nostdsysteminc -fsyntax-only -x c++ -verify=expected,cpp_diagnostics 
-Wmissing-format-attribute 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/llvm/clang/test/Sema/attr-format-missing.c
error: 'cpp_diagnostics-error' diagnostics seen but not expected: 
  File 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/llvm/clang/test/Sema/attr-format-missing.c
 Line 87: subscript of pointer to incomplete type 'void'
  File 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/llvm/clang/test/Sema/attr-format-missing.c
 Line 95: subscript of pointer to incomplete type 'void'
  File 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/llvm/clang/test/Sema/attr-format-missing.c
 Line 96: subscript of pointer to incomplete type 'void'
error: 'cpp_diagnostics-warning' diagnostics expected but not seen: 
  File 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/llvm/clang/test/Sema/attr-format-missing.c
 Line 83 (directive at 
/opt/llvm-buildbot/home/solaris11-sparcv9/clang-solaris11-sparcv9/llvm/clang/test/Sema/attr-format-missing.c:87):
 diagnostic behavior may be improved by adding the 'scanf' format attribute to 
the declaration of 'f7'
4 errors generated.

--




```

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder `sanitizer-ppc64le-linux` 
running on `ppc64le-sanitizer` while building `clang` at step 2 "annotate".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/72/builds/1123

Here is the relevant piece of the build log for the reference:
```
Step 2 (annotate) failure: 'python 
../sanitizer_buildbot/sanitizers/zorg/buildbot/builders/sanitizers/buildbot_selector.py'
 (failure)
...
[110/221] Building CXX object 
tools/clang/lib/Frontend/CMakeFiles/obj.clangFrontend.dir/ChainedIncludesSource.cpp.o
[111/221] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaCast.cpp.o
[112/221] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaAPINotes.cpp.o
[113/221] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseExpr.cpp.o
[114/221] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseObjc.cpp.o
[115/221] Building CXX object 
tools/clang/lib/Interpreter/CMakeFiles/obj.clangInterpreter.dir/CodeCompletion.cpp.o
[116/221] Building CXX object 
tools/clang/tools/c-index-test/CMakeFiles/c-index-test.dir/core_main.cpp.o
[117/221] Building CXX object 
tools/clang/lib/Frontend/CMakeFiles/obj.clangFrontend.dir/ASTUnit.cpp.o
[118/221] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaTemplateInstantiate.cpp.o
[119/221] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o
FAILED: tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o 
CCACHE_CPP2=yes CCACHE_HASHDIR=yes /usr/bin/ccache 
/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm_build0/bin/clang++
 -DGTEST_HAS_RTTI=0 -D_DEBUG -D_GLIBCXX_ASSERTIONS -D_GNU_SOURCE 
-D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS 
-I/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/build_debug/tools/clang/lib/Sema
 
-I/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/clang/lib/Sema
 
-I/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/clang/include
 
-I/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/build_debug/tools/clang/include
 
-I/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/build_debug/include
 
-I/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/llvm/include
 -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror 
-Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra 
-Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers 
-pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough 
-Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor 
-Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion 
-Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color 
-ffunction-sections -fdata-sections -fno-common -Woverloaded-virtual 
-Wno-nested-anon-types -O3 -DNDEBUG -std=c++17  -fno-exceptions -funwind-tables 
-fno-rtti -UNDEBUG -MD -MT 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o -MF 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o.d -o 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o -c 
/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/clang/lib/Sema/SemaDeclAttr.cpp
/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/clang/lib/Sema/SemaDeclAttr.cpp:5529:38:
 error: comparison of integers of different signs: 'int' and 'unsigned int' 
[-Werror,-Wsign-compare]
 5529 |  Attr->getFirstArg() == FirstToCheck;
  |  ~~~ ^  
/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/clang/lib/Sema/SemaDeclAttr.cpp:5528:39:
 error: comparison of integers of different signs: 'int' and 'unsigned int' 
[-Werror,-Wsign-compare]
 5528 |  Attr->getFormatIdx() == StringIndex &&
  |   ^  ~~~
2 errors generated.
[120/221] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParsePragma.cpp.o
[121/221] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseOpenMP.cpp.o
[122/221] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseDeclCXX.cpp.o
[123/221] Building CXX object 
tools/clang/lib/Serialization/CMakeFiles/obj.clangSerialization.dir/ASTWriterStmt.cpp.o
[124/221] Building CXX object 
tools/clang/lib/Frontend/CMakeFiles/obj.clangFrontend.dir/FrontendAction.cpp.o
[125/221] Building CXX object 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder 
`ppc64le-lld-multistage-test` running on `ppc64le-lld-multistage-test` while 
building `clang` at step 12 "build-stage2-unified-tree".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/168/builds/972

Here is the relevant piece of the build log for the reference:
```
Step 12 (build-stage2-unified-tree) failure: build (failure)
...
454.834 [181/569/5420] Building CXX object 
tools/llvm-mca/CMakeFiles/llvm-mca.dir/Views/BottleneckAnalysis.cpp.o
454.893 [181/568/5421] Building CXX object 
tools/lld/ELF/CMakeFiles/lldELF.dir/Arch/X86.cpp.o
454.986 [181/567/5422] Building CXX object 
tools/lld/MachO/CMakeFiles/lldMachO.dir/ICF.cpp.o
455.266 [181/566/5423] Building CXX object 
tools/llvm-reduce/CMakeFiles/llvm-reduce.dir/deltas/ReduceGlobalVars.cpp.o
455.430 [181/565/5424] Building C object 
tools/clang/tools/clang-fuzzer/dictionary/CMakeFiles/clang-fuzzer-dictionary.dir/dictionary.c.o
455.449 [181/564/5425] Building CXX object 
unittests/MC/CMakeFiles/MCTests.dir/StringTableBuilderTest.cpp.o
455.467 [181/563/5426] Building CXX object 
lib/Target/AMDGPU/MCA/CMakeFiles/LLVMAMDGPUTargetMCA.dir/AMDGPUCustomBehaviour.cpp.o
455.521 [181/562/5427] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaLookup.cpp.o
455.534 [181/561/5428] Building CXX object 
tools/llvm-reduce/CMakeFiles/llvm-reduce.dir/deltas/ReduceSpecialGlobals.cpp.o
455.554 [181/560/5429] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o
FAILED: tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o 
ccache 
/home/buildbots/llvm-external-buildbots/workers/ppc64le-lld-multistage-test/ppc64le-lld-multistage-test/install/stage1/bin/clang++
 -DGTEST_HAS_RTTI=0 -D_DEBUG -D_GLIBCXX_ASSERTIONS -D_GNU_SOURCE 
-D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS 
-I/home/buildbots/llvm-external-buildbots/workers/ppc64le-lld-multistage-test/ppc64le-lld-multistage-test/build/stage2/tools/clang/lib/Sema
 
-I/home/buildbots/llvm-external-buildbots/workers/ppc64le-lld-multistage-test/ppc64le-lld-multistage-test/llvm-project/clang/lib/Sema
 
-I/home/buildbots/llvm-external-buildbots/workers/ppc64le-lld-multistage-test/ppc64le-lld-multistage-test/llvm-project/clang/include
 
-I/home/buildbots/llvm-external-buildbots/workers/ppc64le-lld-multistage-test/ppc64le-lld-multistage-test/build/stage2/tools/clang/include
 
-I/home/buildbots/llvm-external-buildbots/workers/ppc64le-lld-multistage-test/ppc64le-lld-multistage-test/build/stage2/include
 
-I/home/buildbots/llvm-external-buildbots/workers/ppc64le-lld-multistage-test/ppc64le-lld-multistage-test/llvm-project/llvm/include
 -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror 
-Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra 
-Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers 
-pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough 
-Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor 
-Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion 
-Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color 
-ffunction-sections -fdata-sections -fno-common -Woverloaded-virtual 
-Wno-nested-anon-types -O3 -DNDEBUG -std=c++17  -fno-exceptions -funwind-tables 
-fno-rtti -UNDEBUG -MD -MT 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o -MF 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o.d -o 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o -c 
/home/buildbots/llvm-external-buildbots/workers/ppc64le-lld-multistage-test/ppc64le-lld-multistage-test/llvm-project/clang/lib/Sema/SemaDeclAttr.cpp
/home/buildbots/llvm-external-buildbots/workers/ppc64le-lld-multistage-test/ppc64le-lld-multistage-test/llvm-project/clang/lib/Sema/SemaDeclAttr.cpp:5529:38:
 error: comparison of integers of different signs: 'int' and 'unsigned int' 
[-Werror,-Wsign-compare]
 5529 |  Attr->getFirstArg() == FirstToCheck;
  |  ~~~ ^  
/home/buildbots/llvm-external-buildbots/workers/ppc64le-lld-multistage-test/ppc64le-lld-multistage-test/llvm-project/clang/lib/Sema/SemaDeclAttr.cpp:5528:39:
 error: comparison of integers of different signs: 'int' and 'unsigned int' 
[-Werror,-Wsign-compare]
 5528 |  Attr->getFormatIdx() == StringIndex &&
  |   ^  ~~~
2 errors generated.
455.610 [181/559/5430] Building CXX object 
tools/bugpoint/CMakeFiles/bugpoint.dir/ExecutionDriver.cpp.o
455.701 [181/558/5431] Building CXX object 
tools/clang/lib/AST/CMakeFiles/obj.clangAST.dir/ASTImporterLookupTable.cpp.o
455.744 [181/557/5432] Building CXX object 
tools/llvm-objdump/CMakeFiles/llvm-objdump.dir/OffloadDump.cpp.o
455.767 [181/556/5433] Building CXX object 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder `sanitizer-aarch64-linux` 
running on `sanitizer-buildbot8` while building `clang` at step 2 "annotate".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/51/builds/1226

Here is the relevant piece of the build log for the reference:
```
Step 2 (annotate) failure: 'python 
../sanitizer_buildbot/sanitizers/zorg/buildbot/builders/sanitizers/buildbot_selector.py'
 (failure)
...
[52/247] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseTemplate.cpp.o
[53/247] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaCUDA.cpp.o
[54/247] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseObjc.cpp.o
[55/247] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseStmt.cpp.o
[56/247] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaAPINotes.cpp.o
[57/247] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseCXXInlineMethods.cpp.o
[58/247] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaARM.cpp.o
[59/247] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseExpr.cpp.o
[60/247] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaAttr.cpp.o
[61/247] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o
FAILED: tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o 
CCACHE_CPP2=yes CCACHE_HASHDIR=yes /usr/bin/ccache 
/b/sanitizer-aarch64-linux/build/llvm_build0/bin/clang++ -DGTEST_HAS_RTTI=0 
-D_DEBUG -D_GLIBCXX_ASSERTIONS -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS 
-D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS 
-I/b/sanitizer-aarch64-linux/build/build_symbolizer/tools/clang/lib/Sema 
-I/b/sanitizer-aarch64-linux/build/llvm-project/clang/lib/Sema 
-I/b/sanitizer-aarch64-linux/build/llvm-project/clang/include 
-I/b/sanitizer-aarch64-linux/build/build_symbolizer/tools/clang/include 
-I/b/sanitizer-aarch64-linux/build/build_symbolizer/include 
-I/b/sanitizer-aarch64-linux/build/llvm-project/llvm/include -fPIC 
-fno-semantic-interposition -fvisibility-inlines-hidden -Werror 
-Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra 
-Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers 
-pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough 
-Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor 
-Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion 
-Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color 
-ffunction-sections -fdata-sections -fno-common -Woverloaded-virtual 
-Wno-nested-anon-types -O3 -DNDEBUG -std=c++17  -fno-exceptions -funwind-tables 
-fno-rtti -UNDEBUG -MD -MT 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o -MF 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o.d -o 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o -c 
/b/sanitizer-aarch64-linux/build/llvm-project/clang/lib/Sema/SemaDeclAttr.cpp
/b/sanitizer-aarch64-linux/build/llvm-project/clang/lib/Sema/SemaDeclAttr.cpp:5529:38:
 error: comparison of integers of different signs: 'int' and 'unsigned int' 
[-Werror,-Wsign-compare]
 5529 |  Attr->getFirstArg() == FirstToCheck;
  |  ~~~ ^  
/b/sanitizer-aarch64-linux/build/llvm-project/clang/lib/Sema/SemaDeclAttr.cpp:5528:39:
 error: comparison of integers of different signs: 'int' and 'unsigned int' 
[-Werror,-Wsign-compare]
 5528 |  Attr->getFormatIdx() == StringIndex &&
  |   ^  ~~~
2 errors generated.
[62/247] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseStmtAsm.cpp.o
[63/247] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaCast.cpp.o
[64/247] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseDeclCXX.cpp.o
[65/247] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseOpenMP.cpp.o
[66/247] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaM68k.cpp.o
[67/247] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaExceptionSpec.cpp.o
[68/247] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaMSP430.cpp.o
[69/247] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParsePragma.cpp.o
[70/247] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseDecl.cpp.o
[71/247] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaHexagon.cpp.o
[72/247] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/ParsedAttr.cpp.o
[73/247] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaFixItUtils.cpp.o
[74/247] 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder `sanitizer-x86_64-linux` 
running on `sanitizer-buildbot1` while building `clang` at step 2 "annotate".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/66/builds/1409

Here is the relevant piece of the build log for the reference:
```
Step 2 (annotate) failure: 'python 
../sanitizer_buildbot/sanitizers/zorg/buildbot/builders/sanitizers/buildbot_selector.py'
 (failure)
...
[78/248] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseTemplate.cpp.o
[79/248] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseStmtAsm.cpp.o
[80/248] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/Parser.cpp.o
[81/248] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseExprCXX.cpp.o
[82/248] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaObjCProperty.cpp.o
[83/248] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaAPINotes.cpp.o
[84/248] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaExprMember.cpp.o
[85/248] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaAttr.cpp.o
[86/248] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaObjC.cpp.o
[87/248] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o
FAILED: tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o 
CCACHE_CPP2=yes CCACHE_HASHDIR=yes /usr/bin/ccache 
/b/sanitizer-x86_64-linux/build/llvm_build0/bin/clang++ -DGTEST_HAS_RTTI=0 
-D_DEBUG -D_GLIBCXX_ASSERTIONS -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS 
-D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS 
-I/b/sanitizer-x86_64-linux/build/build_symbolizer/tools/clang/lib/Sema 
-I/b/sanitizer-x86_64-linux/build/llvm-project/clang/lib/Sema 
-I/b/sanitizer-x86_64-linux/build/llvm-project/clang/include 
-I/b/sanitizer-x86_64-linux/build/build_symbolizer/tools/clang/include 
-I/b/sanitizer-x86_64-linux/build/build_symbolizer/include 
-I/b/sanitizer-x86_64-linux/build/llvm-project/llvm/include -fPIC 
-fno-semantic-interposition -fvisibility-inlines-hidden -Werror 
-Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra 
-Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers 
-pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough 
-Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor 
-Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion 
-Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color 
-ffunction-sections -fdata-sections -fno-common -Woverloaded-virtual 
-Wno-nested-anon-types -O3 -DNDEBUG -std=c++17  -fno-exceptions -funwind-tables 
-fno-rtti -UNDEBUG -MD -MT 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o -MF 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o.d -o 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaDeclAttr.cpp.o -c 
/b/sanitizer-x86_64-linux/build/llvm-project/clang/lib/Sema/SemaDeclAttr.cpp
/b/sanitizer-x86_64-linux/build/llvm-project/clang/lib/Sema/SemaDeclAttr.cpp:5529:38:
 error: comparison of integers of different signs: 'int' and 'unsigned int' 
[-Werror,-Wsign-compare]
 5529 |  Attr->getFirstArg() == FirstToCheck;
  |  ~~~ ^  
/b/sanitizer-x86_64-linux/build/llvm-project/clang/lib/Sema/SemaDeclAttr.cpp:5528:39:
 error: comparison of integers of different signs: 'int' and 'unsigned int' 
[-Werror,-Wsign-compare]
 5528 |  Attr->getFormatIdx() == StringIndex &&
  |   ^  ~~~
2 errors generated.
[88/248] Linking CXX executable bin/apinotes-test
[89/248] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseExpr.cpp.o
[90/248] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseDeclCXX.cpp.o
[91/248] Building CXX object 
tools/clang/tools/diagtool/CMakeFiles/diagtool.dir/ListWarnings.cpp.o
[92/248] Building CXX object 
tools/clang/tools/diagtool/CMakeFiles/diagtool.dir/FindDiagnosticID.cpp.o
[93/248] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaWasm.cpp.o
[94/248] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParseOpenMP.cpp.o
[95/248] Building CXX object 
tools/clang/tools/diagtool/CMakeFiles/diagtool.dir/DiagnosticNames.cpp.o
[96/248] Building CXX object 
tools/clang/tools/diagtool/CMakeFiles/diagtool.dir/TreeView.cpp.o
[97/248] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaX86.cpp.o
[98/248] Building CXX object 
tools/clang/lib/Parse/CMakeFiles/obj.clangParse.dir/ParsePragma.cpp.o
[99/248] Building CXX object 
tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaInit.cpp.o
[100/248] Building CXX object 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder `clang-armv8-quick` running 
on `linaro-clang-armv8-quick` while building `clang` at step 5 "ninja check 1".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/154/builds/1294

Here is the relevant piece of the build log for the reference:
```
Step 5 (ninja check 1) failure: stage 1 checked (failure)
 TEST 'Clang :: Sema/attr-format-missing.c' FAILED 

Exit Code: 1

Command Output (stderr):
--
RUN: at line 1: /home/tcwg-buildbot/worker/clang-armv8-quick/stage1/bin/clang 
-cc1 -internal-isystem 
/home/tcwg-buildbot/worker/clang-armv8-quick/stage1/lib/clang/19/include 
-nostdsysteminc -fsyntax-only -verify=expected,c_diagnostics 
-Wmissing-format-attribute 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c
+ /home/tcwg-buildbot/worker/clang-armv8-quick/stage1/bin/clang -cc1 
-internal-isystem 
/home/tcwg-buildbot/worker/clang-armv8-quick/stage1/lib/clang/19/include 
-nostdsysteminc -fsyntax-only -verify=expected,c_diagnostics 
-Wmissing-format-attribute 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c
error: 'c_diagnostics-error' diagnostics seen but not expected: 
  File 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 87: subscripted value is not an array, pointer, or vector
  File 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 95: subscripted value is not an array, pointer, or vector
  File 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 96: subscripted value is not an array, pointer, or vector
  File 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 391: passing 'char *' to parameter of incompatible type 
'__builtin_va_list'
error: 'c_diagnostics-warning' diagnostics expected but not seen: 
  File 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 83 (directive at 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c:87):
 diagnostic behavior may be improved by adding the 'scanf' format attribute to 
the declaration of 'f7'
  File 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 111 (directive at 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c:113):
 incompatible pointer types passing 'const wchar_t *' (aka 'const int *') to 
parameter of type 'const char *'
  File 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 141 (directive at 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c:143):
 incompatible pointer types passing 'const wchar_t *' (aka 'const int *') to 
parameter of type 'const char *'
error: 'c_diagnostics-warning' diagnostics seen but not expected: 
  File 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 111: incompatible pointer types passing 'const wchar_t *' (aka 'const 
unsigned int *') to parameter of type 'const char *'
  File 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 141: incompatible pointer types passing 'const wchar_t *' (aka 'const 
unsigned int *') to parameter of type 'const char *'
  File 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 387: diagnostic behavior may be improved by adding the 'printf' format 
attribute to the declaration of 'f41'
error: 'c_diagnostics-note' diagnostics seen but not expected: 
  File 
/home/tcwg-buildbot/worker/clang-armv8-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 30: passing argument to parameter here
11 errors generated.

--




```

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder `clang-aarch64-quick` 
running on `linaro-clang-aarch64-quick` while building `clang` at step 5 "ninja 
check 1".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/65/builds/1303

Here is the relevant piece of the build log for the reference:
```
Step 5 (ninja check 1) failure: stage 1 checked (failure)
 TEST 'Clang :: Sema/attr-format-missing.c' FAILED 

Exit Code: 1

Command Output (stderr):
--
RUN: at line 1: /home/tcwg-buildbot/worker/clang-aarch64-quick/stage1/bin/clang 
-cc1 -internal-isystem 
/home/tcwg-buildbot/worker/clang-aarch64-quick/stage1/lib/clang/19/include 
-nostdsysteminc -fsyntax-only -verify=expected,c_diagnostics 
-Wmissing-format-attribute 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c
+ /home/tcwg-buildbot/worker/clang-aarch64-quick/stage1/bin/clang -cc1 
-internal-isystem 
/home/tcwg-buildbot/worker/clang-aarch64-quick/stage1/lib/clang/19/include 
-nostdsysteminc -fsyntax-only -verify=expected,c_diagnostics 
-Wmissing-format-attribute 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c
error: 'c_diagnostics-error' diagnostics seen but not expected: 
  File 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 87: subscripted value is not an array, pointer, or vector
  File 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 95: subscripted value is not an array, pointer, or vector
  File 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 96: subscripted value is not an array, pointer, or vector
  File 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 391: passing 'char *' to parameter of incompatible type 
'__builtin_va_list'
error: 'c_diagnostics-warning' diagnostics expected but not seen: 
  File 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 83 (directive at 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c:87):
 diagnostic behavior may be improved by adding the 'scanf' format attribute to 
the declaration of 'f7'
  File 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 111 (directive at 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c:113):
 incompatible pointer types passing 'const wchar_t *' (aka 'const int *') to 
parameter of type 'const char *'
  File 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 141 (directive at 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c:143):
 incompatible pointer types passing 'const wchar_t *' (aka 'const int *') to 
parameter of type 'const char *'
error: 'c_diagnostics-warning' diagnostics seen but not expected: 
  File 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 111: incompatible pointer types passing 'const wchar_t *' (aka 'const 
unsigned int *') to parameter of type 'const char *'
  File 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 141: incompatible pointer types passing 'const wchar_t *' (aka 'const 
unsigned int *') to parameter of type 'const char *'
  File 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 387: diagnostic behavior may be improved by adding the 'printf' format 
attribute to the declaration of 'f41'
error: 'c_diagnostics-note' diagnostics seen but not expected: 
  File 
/home/tcwg-buildbot/worker/clang-aarch64-quick/llvm/clang/test/Sema/attr-format-missing.c
 Line 30: passing argument to parameter here
11 errors generated.

--




```

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder `llvm-clang-x86_64-sie-win` 
running on `sie-win-worker` while building `clang` at step 7 
"test-build-unified-tree-check-all".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/46/builds/1528

Here is the relevant piece of the build log for the reference:
```
Step 7 (test-build-unified-tree-check-all) failure: test (failure)
 TEST 'Clang :: Sema/attr-format-missing.c' FAILED 

Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
z:\b\llvm-clang-x86_64-sie-win\build\bin\clang.exe -cc1 -internal-isystem 
Z:\b\llvm-clang-x86_64-sie-win\build\lib\clang\19\include -nostdsysteminc 
-fsyntax-only -verify=expected,c_diagnostics -Wmissing-format-attribute 
Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Sema\attr-format-missing.c
# executed command: 'z:\b\llvm-clang-x86_64-sie-win\build\bin\clang.exe' -cc1 
-internal-isystem 'Z:\b\llvm-clang-x86_64-sie-win\build\lib\clang\19\include' 
-nostdsysteminc -fsyntax-only -verify=expected,c_diagnostics 
-Wmissing-format-attribute 
'Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Sema\attr-format-missing.c'
# .---command stderr
# | error: 'c_diagnostics-warning' diagnostics seen but not expected: 
# |   File 
Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Sema\attr-format-missing.c
 Line 391: incompatible pointer types passing 'char *' to parameter of type 
'struct __va_list_tag *'
# | error: 'c_diagnostics-note' diagnostics seen but not expected: 
# |   File 
Z:\b\llvm-clang-x86_64-sie-win\llvm-project\clang\test\Sema\attr-format-missing.c
 Line 30: passing argument to parameter here
# | 2 errors generated.
# `-
# error: command failed with exit status: 1

--




```

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder 
`llvm-clang-x86_64-sie-ubuntu-fast` running on `sie-linux-worker` while 
building `clang` at step 6 "test-build-unified-tree-check-all".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/144/builds/2140

Here is the relevant piece of the build log for the reference:
```
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
 TEST 'Clang :: Sema/attr-format-missing.c' FAILED 

Exit Code: 1

Command Output (stderr):
--
RUN: at line 1: 
/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/clang 
-cc1 -internal-isystem 
/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/lib/clang/19/include
 -nostdsysteminc -fsyntax-only -verify=expected,c_diagnostics 
-Wmissing-format-attribute 
/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/Sema/attr-format-missing.c
+ 
/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/clang 
-cc1 -internal-isystem 
/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/lib/clang/19/include
 -nostdsysteminc -fsyntax-only -verify=expected,c_diagnostics 
-Wmissing-format-attribute 
/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/Sema/attr-format-missing.c
error: 'c_diagnostics-warning' diagnostics seen but not expected: 
  File 
/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/Sema/attr-format-missing.c
 Line 391: incompatible pointer types passing 'char *' to parameter of type 
'struct __va_list_tag *'
error: 'c_diagnostics-note' diagnostics seen but not expected: 
  File 
/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/clang/test/Sema/attr-format-missing.c
 Line 30: passing argument to parameter here
2 errors generated.

--




```

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-12 Thread Djordje Todorovic via cfe-commits

https://github.com/djtodoro closed 
https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-11 Thread Aaron Ballman via cfe-commits

https://github.com/AaronBallman approved this pull request.

LGTM, thank you!

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-11 Thread Budimir Aranđelović via cfe-commits


@@ -0,0 +1,277 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+
+typedef unsigned short char16_t;
+typedef unsigned int char32_t;
+typedef __WCHAR_TYPE__ wchar_t;
+typedef __SIZE_TYPE__ size_t;
+typedef __builtin_va_list va_list;
+
+__attribute__((__format__(__printf__, 1, 2)))
+int printf(const char *, ...); // #printf
+
+__attribute__((__format__(__scanf__, 1, 2)))
+int scanf(const char *, ...); // #scanf
+
+__attribute__((__format__(__printf__, 1, 0)))
+int vprintf(const char *, va_list); // #vprintf
+
+__attribute__((__format__(__scanf__, 1, 0)))
+int vscanf(const char *, va_list); // #vscanf
+
+__attribute__((__format__(__printf__, 2, 0)))
+int vsprintf(char *, const char *, va_list); // #vsprintf
+
+__attribute__((__format__(__printf__, 3, 0)))
+int vsnprintf(char *ch, size_t, const char *, va_list); // #vsnprintf
+
+__attribute__((__format__(__scanf__, 1, 4)))
+void f1(char *out, const size_t len, const char *format, ... /* args */) // #f1
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning@#f1 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f1'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 3, 4)))
+}
+
+__attribute__((__format__(__printf__, 1, 4)))
+void f2(char *out, const size_t len, const char *format, ... /* args */) // #f2
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning@#f2 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f2'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 3, 4)))
+}
+
+void f3(char *out, va_list args) // #f3
+{
+vprintf(out, args); // expected-warning@#f3 {{diagnostic behavior may be 
improved by adding the 'printf' format attribute to the declaration of 'f3'}}
+// CHECK-FIXES: __attribute__((format(printf, 1, 0)))
+vscanf(out, args); // expected-warning@#f3 {{diagnostic behavior may be 
improved by adding the 'scanf' format attribute to the declaration of 'f3'}}
+   // CHECK-FIXES: __attribute__((format(scanf, 1, 0)))

budimirarandjelovicsyrmia wrote:

Update: this function diagnoses format attributes for Linux 64. I found that 
after posting previous comment. Apart of this, I did not make additional changes

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-11 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 569ecf2818c47b355fc1130fccb611e0d40f21a2 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Attr.h   |   7 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaDecl.cpp   |   2 +
 clang/lib/Sema/SemaDeclAttr.cpp   | 219 +-
 clang/test/Sema/attr-format-missing.c | 403 ++
 clang/test/Sema/attr-format-missing.cpp   | 174 
 9 files changed, 813 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6adf57da42e65..f9fd4a20ea6bf 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -707,6 +707,9 @@ Improvements to Clang's diagnostics
 
 - Clang now diagnoses integer constant expressions that are folded to a 
constant value as an extension in more circumstances. Fixes #GH59863
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH60718
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 2241f8481484e..da6a3b2fe3571 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -525,7 +525,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def MissingIncludeDirs : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 940f9ac226ca8..75e51c5aa166d 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1031,6 +1031,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Attr.h b/clang/include/clang/Sema/Attr.h
index 3f0b10212789a..37c124ca7b454 100644
--- a/clang/include/clang/Sema/Attr.h
+++ b/clang/include/clang/Sema/Attr.h
@@ -123,6 +123,13 @@ inline bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+inline bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 /// Diagnose mutually exclusive attributes when present on a given
 /// declaration. Returns true if diagnosed.
 template 
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 57994f4033922..9022ac86edf81 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -4602,6 +4602,10 @@ class Sema final : public SemaBase {
 
   enum class RetainOwnershipKind { NS, CF, OS };
 
+  void DiagnoseMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+  std::vector
+  GetMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 66eeaa8e6f777..29c28d4b56751 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -15925,6 +15925,8 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt 
*Body,
 }
   }
 
+  DiagnoseMissingFormatAttributes(Body, FD);
+
   // We might not have found a prototype because we didn't wish to warn on
   // the lack of a missing prototype. Try again without the checks for
   // whether we want to warn on the missing prototype.
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-10 Thread Aaron Ballman via cfe-commits


@@ -0,0 +1,277 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+
+typedef unsigned short char16_t;
+typedef unsigned int char32_t;
+typedef __WCHAR_TYPE__ wchar_t;
+typedef __SIZE_TYPE__ size_t;
+typedef __builtin_va_list va_list;
+
+__attribute__((__format__(__printf__, 1, 2)))
+int printf(const char *, ...); // #printf
+
+__attribute__((__format__(__scanf__, 1, 2)))
+int scanf(const char *, ...); // #scanf
+
+__attribute__((__format__(__printf__, 1, 0)))
+int vprintf(const char *, va_list); // #vprintf
+
+__attribute__((__format__(__scanf__, 1, 0)))
+int vscanf(const char *, va_list); // #vscanf
+
+__attribute__((__format__(__printf__, 2, 0)))
+int vsprintf(char *, const char *, va_list); // #vsprintf
+
+__attribute__((__format__(__printf__, 3, 0)))
+int vsnprintf(char *ch, size_t, const char *, va_list); // #vsnprintf
+
+__attribute__((__format__(__scanf__, 1, 4)))
+void f1(char *out, const size_t len, const char *format, ... /* args */) // #f1
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning@#f1 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f1'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 3, 4)))
+}
+
+__attribute__((__format__(__printf__, 1, 4)))
+void f2(char *out, const size_t len, const char *format, ... /* args */) // #f2
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning@#f2 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f2'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 3, 4)))
+}
+
+void f3(char *out, va_list args) // #f3
+{
+vprintf(out, args); // expected-warning@#f3 {{diagnostic behavior may be 
improved by adding the 'printf' format attribute to the declaration of 'f3'}}
+// CHECK-FIXES: __attribute__((format(printf, 1, 0)))
+vscanf(out, args); // expected-warning@#f3 {{diagnostic behavior may be 
improved by adding the 'scanf' format attribute to the declaration of 'f3'}}
+   // CHECK-FIXES: __attribute__((format(scanf, 1, 0)))

AaronBallman wrote:

I think we *could* do that, but I'm not certain it's worth the effort as that's 
a pretty unusual situation. I think it's reasonable to not emit the diagnostic 
on that code even though it plausibly could make sense to.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-10 Thread Aaron Ballman via cfe-commits

AaronBallman wrote:

Pre-commit CI seems to have found a valid failure:
```
_bk;t=1720616429374FAIL: Clang :: Sema/attr-format-missing.c (16228 of 20429)
_bk;t=1720616429374 TEST 'Clang :: 
Sema/attr-format-missing.c' FAILED 
_bk;t=1720616429374Exit Code: 1
_bk;t=1720616429374
_bk;t=1720616429374Command Output (stdout):
_bk;t=1720616429374--
_bk;t=1720616429374# RUN: at line 1
_bk;t=1720616429374c:\ws\src\build\bin\clang.exe -cc1 -internal-isystem 
C:\ws\src\build\lib\clang\19\include -nostdsysteminc -fsyntax-only 
-verify=expected,c_diagnostics -Wmissing-format-attribute 
C:\ws\src\clang\test\Sema\attr-format-missing.c
_bk;t=1720616429374# executed command: 'c:\ws\src\build\bin\clang.exe' -cc1 
-internal-isystem 'C:\ws\src\build\lib\clang\19\include' -nostdsysteminc 
-fsyntax-only -verify=expected,c_diagnostics -Wmissing-format-attribute 
'C:\ws\src\clang\test\Sema\attr-format-missing.c'
_bk;t=1720616429374# note: command had no output on stdout or stderr
_bk;t=1720616429374# RUN: at line 2
_bk;t=1720616429374c:\ws\src\build\bin\clang.exe -cc1 -internal-isystem 
C:\ws\src\build\lib\clang\19\include -nostdsysteminc -fsyntax-only 
-Wmissing-format-attribute -fdiagnostics-parseable-fixits 
C:\ws\src\clang\test\Sema\attr-format-missing.c 2>&1 | 
c:\ws\src\build\bin\filecheck.exe 
C:\ws\src\clang\test\Sema\attr-format-missing.c --check-prefixes=CHECK,C-CHECK
_bk;t=1720616429374# executed command: 'c:\ws\src\build\bin\clang.exe' -cc1 
-internal-isystem 'C:\ws\src\build\lib\clang\19\include' -nostdsysteminc 
-fsyntax-only -Wmissing-format-attribute -fdiagnostics-parseable-fixits 
'C:\ws\src\clang\test\Sema\attr-format-missing.c'
_bk;t=1720616429374# note: command had no output on stdout or stderr
_bk;t=1720616429374# executed command: 'c:\ws\src\build\bin\filecheck.exe' 
'C:\ws\src\clang\test\Sema\attr-format-missing.c' --check-prefixes=CHECK,C-CHECK
_bk;t=1720616429374# note: command had no output on stdout or stderr
_bk;t=1720616429374# RUN: at line 3
_bk;t=1720616429374c:\ws\src\build\bin\clang.exe -cc1 -internal-isystem 
C:\ws\src\build\lib\clang\19\include -nostdsysteminc -fsyntax-only -x c++ 
-verify=expected,cpp_diagnostics -Wmissing-format-attribute 
C:\ws\src\clang\test\Sema\attr-format-missing.c
_bk;t=1720616429374# executed command: 'c:\ws\src\build\bin\clang.exe' -cc1 
-internal-isystem 'C:\ws\src\build\lib\clang\19\include' -nostdsysteminc 
-fsyntax-only -x c++ -verify=expected,cpp_diagnostics 
-Wmissing-format-attribute 'C:\ws\src\clang\test\Sema\attr-format-missing.c'
_bk;t=1720616429374# note: command had no output on stdout or stderr
_bk;t=1720616429374# RUN: at line 4
_bk;t=1720616429374c:\ws\src\build\bin\clang.exe -cc1 -internal-isystem 
C:\ws\src\build\lib\clang\19\include -nostdsysteminc -fsyntax-only -x c++ 
-verify=expected,cpp_diagnostics -std=c++2b -Wmissing-format-attribute 
C:\ws\src\clang\test\Sema\attr-format-missing.c
_bk;t=1720616429374# executed command: 'c:\ws\src\build\bin\clang.exe' -cc1 
-internal-isystem 'C:\ws\src\build\lib\clang\19\include' -nostdsysteminc 
-fsyntax-only -x c++ -verify=expected,cpp_diagnostics -std=c++2b 
-Wmissing-format-attribute 'C:\ws\src\clang\test\Sema\attr-format-missing.c'
_bk;t=1720616429374# note: command had no output on stdout or stderr
_bk;t=1720616429374# RUN: at line 5
_bk;t=1720616429374c:\ws\src\build\bin\clang.exe -cc1 -internal-isystem 
C:\ws\src\build\lib\clang\19\include -nostdsysteminc -fsyntax-only -x c++ 
-verify=expected,cpp_diagnostics -std=c++23 -Wmissing-format-attribute 
C:\ws\src\clang\test\Sema\attr-format-missing.c
_bk;t=1720616429374# executed command: 'c:\ws\src\build\bin\clang.exe' -cc1 
-internal-isystem 'C:\ws\src\build\lib\clang\19\include' -nostdsysteminc 
-fsyntax-only -x c++ -verify=expected,cpp_diagnostics -std=c++23 
-Wmissing-format-attribute 'C:\ws\src\clang\test\Sema\attr-format-missing.c'
_bk;t=1720616429374# note: command had no output on stdout or stderr
_bk;t=1720616429374# RUN: at line 6
_bk;t=1720616429374not c:\ws\src\build\bin\clang.exe -cc1 -internal-isystem 
C:\ws\src\build\lib\clang\19\include -nostdsysteminc -fsyntax-only -x c++ 
-Wmissing-format-attribute -fdiagnostics-parseable-fixits 
C:\ws\src\clang\test\Sema\attr-format-missing.c 2>&1 | 
c:\ws\src\build\bin\filecheck.exe 
C:\ws\src\clang\test\Sema\attr-format-missing.c --check-prefixes=CHECK,CPP-CHECK
_bk;t=1720616429374# executed command: not 'c:\ws\src\build\bin\clang.exe' 
-cc1 -internal-isystem 'C:\ws\src\build\lib\clang\19\include' -nostdsysteminc 
-fsyntax-only -x c++ -Wmissing-format-attribute -fdiagnostics-parseable-fixits 
'C:\ws\src\clang\test\Sema\attr-format-missing.c'
_bk;t=1720616429374# note: command had no output on stdout or stderr
_bk;t=1720616429374# executed command: 'c:\ws\src\build\bin\filecheck.exe' 
'C:\ws\src\clang\test\Sema\attr-format-missing.c' 
--check-prefixes=CHECK,CPP-CHECK
_bk;t=1720616429374# 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-10 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 2abeecc9379df7c87dc32ba458a4dd79c3aabb78 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Attr.h   |   7 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaDecl.cpp   |   2 +
 clang/lib/Sema/SemaDeclAttr.cpp   | 219 +-
 clang/test/Sema/attr-format-missing.c | 400 ++
 clang/test/Sema/attr-format-missing.cpp   | 174 
 9 files changed, 810 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index cd3a4c2b1be1a..3fafaf0a5b2c2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -686,6 +686,9 @@ Improvements to Clang's diagnostics
 - Clang no longer emits a "no previous prototype" warning for Win32 entry 
points under ``-Wmissing-prototypes``.
   Fixes #GH94366.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH60718
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 2241f8481484e..da6a3b2fe3571 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -525,7 +525,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def MissingIncludeDirs : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index fa02622f12271..a09bcba217cde 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1028,6 +1028,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Attr.h b/clang/include/clang/Sema/Attr.h
index 3f0b10212789a..37c124ca7b454 100644
--- a/clang/include/clang/Sema/Attr.h
+++ b/clang/include/clang/Sema/Attr.h
@@ -123,6 +123,13 @@ inline bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+inline bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 /// Diagnose mutually exclusive attributes when present on a given
 /// declaration. Returns true if diagnosed.
 template 
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 57994f4033922..9022ac86edf81 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -4602,6 +4602,10 @@ class Sema final : public SemaBase {
 
   enum class RetainOwnershipKind { NS, CF, OS };
 
+  void DiagnoseMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+  std::vector
+  GetMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index d1c7b9d5ae507..ac31de5ced827 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -15924,6 +15924,8 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt 
*Body,
 }
   }
 
+  DiagnoseMissingFormatAttributes(Body, FD);
+
   // We might not have found a prototype because we didn't wish to warn on
   // the lack of a missing prototype. Try again without the checks for
   // whether we want to warn on the missing prototype.
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-10 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 30d1b5012de980b0933011881fd44edc367082af Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Attr.h   |   7 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaDecl.cpp   |   2 +
 clang/lib/Sema/SemaDeclAttr.cpp   | 219 +-
 clang/test/Sema/attr-format-missing.c | 400 ++
 clang/test/Sema/attr-format-missing.cpp   | 174 
 9 files changed, 810 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index cd3a4c2b1be1a..3fafaf0a5b2c2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -686,6 +686,9 @@ Improvements to Clang's diagnostics
 - Clang no longer emits a "no previous prototype" warning for Win32 entry 
points under ``-Wmissing-prototypes``.
   Fixes #GH94366.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH60718
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 2241f8481484e..da6a3b2fe3571 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -525,7 +525,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def MissingIncludeDirs : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index fa02622f12271..a09bcba217cde 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1028,6 +1028,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Attr.h b/clang/include/clang/Sema/Attr.h
index 3f0b10212789a..37c124ca7b454 100644
--- a/clang/include/clang/Sema/Attr.h
+++ b/clang/include/clang/Sema/Attr.h
@@ -123,6 +123,13 @@ inline bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+inline bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 /// Diagnose mutually exclusive attributes when present on a given
 /// declaration. Returns true if diagnosed.
 template 
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 57994f4033922..9022ac86edf81 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -4602,6 +4602,10 @@ class Sema final : public SemaBase {
 
   enum class RetainOwnershipKind { NS, CF, OS };
 
+  void DiagnoseMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+  std::vector
+  GetMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index d1c7b9d5ae507..ac31de5ced827 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -15924,6 +15924,8 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt 
*Body,
 }
   }
 
+  DiagnoseMissingFormatAttributes(Body, FD);
+
   // We might not have found a prototype because we didn't wish to warn on
   // the lack of a missing prototype. Try again without the checks for
   // whether we want to warn on the missing prototype.
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-10 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 1d184ff69cfdbb94f7b3e829d8f2e7aae13a6aaf Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Attr.h   |   7 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaDecl.cpp   |   2 +
 clang/lib/Sema/SemaDeclAttr.cpp   | 219 +-
 clang/test/Sema/attr-format-missing.c | 394 ++
 clang/test/Sema/attr-format-missing.cpp   | 174 
 9 files changed, 804 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index cd3a4c2b1be1a..3fafaf0a5b2c2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -686,6 +686,9 @@ Improvements to Clang's diagnostics
 - Clang no longer emits a "no previous prototype" warning for Win32 entry 
points under ``-Wmissing-prototypes``.
   Fixes #GH94366.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH60718
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 2241f8481484e..da6a3b2fe3571 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -525,7 +525,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def MissingIncludeDirs : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index fa02622f12271..a09bcba217cde 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1028,6 +1028,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Attr.h b/clang/include/clang/Sema/Attr.h
index 3f0b10212789a..37c124ca7b454 100644
--- a/clang/include/clang/Sema/Attr.h
+++ b/clang/include/clang/Sema/Attr.h
@@ -123,6 +123,13 @@ inline bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+inline bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 /// Diagnose mutually exclusive attributes when present on a given
 /// declaration. Returns true if diagnosed.
 template 
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 57994f4033922..9022ac86edf81 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -4602,6 +4602,10 @@ class Sema final : public SemaBase {
 
   enum class RetainOwnershipKind { NS, CF, OS };
 
+  void DiagnoseMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+  std::vector
+  GetMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index d1c7b9d5ae507..ac31de5ced827 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -15924,6 +15924,8 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt 
*Body,
 }
   }
 
+  DiagnoseMissingFormatAttributes(Body, FD);
+
   // We might not have found a prototype because we didn't wish to warn on
   // the lack of a missing prototype. Try again without the checks for
   // whether we want to warn on the missing prototype.
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-09 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia edited 
https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-09 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 34913edf1a2b1f294ed7a70d27ce772aca41e3dd Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Attr.h   |   7 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaDecl.cpp   |   2 +
 clang/lib/Sema/SemaDeclAttr.cpp   | 219 +-
 clang/test/Sema/attr-format-missing.c | 394 ++
 clang/test/Sema/attr-format-missing.cpp   | 174 
 9 files changed, 804 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e3ed18e1b288a..b5201d9f0a5ad 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -665,6 +665,9 @@ Improvements to Clang's diagnostics
 
 - Clang now shows implicit deduction guides when diagnosing overload 
resolution failure. #GH92393.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH60718
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 2241f8481484e..da6a3b2fe3571 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -525,7 +525,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def MissingIncludeDirs : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1aba8bc24ba2f..955210e8d74b7 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1028,6 +1028,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Attr.h b/clang/include/clang/Sema/Attr.h
index 3f0b10212789a..37c124ca7b454 100644
--- a/clang/include/clang/Sema/Attr.h
+++ b/clang/include/clang/Sema/Attr.h
@@ -123,6 +123,13 @@ inline bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+inline bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 /// Diagnose mutually exclusive attributes when present on a given
 /// declaration. Returns true if diagnosed.
 template 
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 75a80540dbcbf..708c9a9288905 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -4602,6 +4602,10 @@ class Sema final : public SemaBase {
 
   enum class RetainOwnershipKind { NS, CF, OS };
 
+  void DiagnoseMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+  std::vector
+  GetMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index aa44608035538..d64c418ea40de 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -15921,6 +15921,8 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt 
*Body,
 }
   }
 
+  DiagnoseMissingFormatAttributes(Body, FD);
+
   // We might not have found a prototype because we didn't wish to warn on
   // the lack of a missing prototype. Try again without the checks for
   // whether we want to warn on the missing prototype.
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-09 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia edited 
https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-09 Thread Budimir Aranđelović via cfe-commits


@@ -0,0 +1,277 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+
+typedef unsigned short char16_t;
+typedef unsigned int char32_t;
+typedef __WCHAR_TYPE__ wchar_t;
+typedef __SIZE_TYPE__ size_t;
+typedef __builtin_va_list va_list;
+
+__attribute__((__format__(__printf__, 1, 2)))
+int printf(const char *, ...); // #printf
+
+__attribute__((__format__(__scanf__, 1, 2)))
+int scanf(const char *, ...); // #scanf
+
+__attribute__((__format__(__printf__, 1, 0)))
+int vprintf(const char *, va_list); // #vprintf
+
+__attribute__((__format__(__scanf__, 1, 0)))
+int vscanf(const char *, va_list); // #vscanf
+
+__attribute__((__format__(__printf__, 2, 0)))
+int vsprintf(char *, const char *, va_list); // #vsprintf
+
+__attribute__((__format__(__printf__, 3, 0)))
+int vsnprintf(char *ch, size_t, const char *, va_list); // #vsnprintf
+
+__attribute__((__format__(__scanf__, 1, 4)))
+void f1(char *out, const size_t len, const char *format, ... /* args */) // #f1
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning@#f1 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f1'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 3, 4)))
+}
+
+__attribute__((__format__(__printf__, 1, 4)))
+void f2(char *out, const size_t len, const char *format, ... /* args */) // #f2
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning@#f2 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f2'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 3, 4)))
+}
+
+void f3(char *out, va_list args) // #f3
+{
+vprintf(out, args); // expected-warning@#f3 {{diagnostic behavior may be 
improved by adding the 'printf' format attribute to the declaration of 'f3'}}
+// CHECK-FIXES: __attribute__((format(printf, 1, 0)))
+vscanf(out, args); // expected-warning@#f3 {{diagnostic behavior may be 
improved by adding the 'scanf' format attribute to the declaration of 'f3'}}
+   // CHECK-FIXES: __attribute__((format(scanf, 1, 0)))

budimirarandjelovicsyrmia wrote:

I edited code so now it does not emit diagnostic when there are different 
format functions in the same function body. However, I wonder if diagnostic 
could be emitted in this situation:
`void f41(char *out, ... /* args */) // #f41
{
va_list args;
char *ch;
vscanf("%s", ch); // expected-no-warning@#f41
vprintf(out, args); // expected-no-warning@#f41
}`
Currently, there is no diagnostic due to different format functions. If there 
were only one format function (ie. either vscanf or vprintf), diagnostic would 
be emitted only for latter one (vprintf). As there are more than one format 
function and va_list only appears as argument in one of them, does it make 
sense to emit diagnostic for one where format argument appears?

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-09 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 20932c9fc48a2c320058ec48baa065fd9e59f801 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Attr.h   |   7 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaDecl.cpp   |   2 +
 clang/lib/Sema/SemaDeclAttr.cpp   | 215 +-
 clang/test/Sema/attr-format-missing.c | 390 ++
 clang/test/Sema/attr-format-missing.cpp   | 174 
 9 files changed, 796 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e3ed18e1b288a..b5201d9f0a5ad 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -665,6 +665,9 @@ Improvements to Clang's diagnostics
 
 - Clang now shows implicit deduction guides when diagnosing overload 
resolution failure. #GH92393.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH60718
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 2241f8481484e..da6a3b2fe3571 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -525,7 +525,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def MissingIncludeDirs : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1aba8bc24ba2f..955210e8d74b7 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1028,6 +1028,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Attr.h b/clang/include/clang/Sema/Attr.h
index 3f0b10212789a..37c124ca7b454 100644
--- a/clang/include/clang/Sema/Attr.h
+++ b/clang/include/clang/Sema/Attr.h
@@ -123,6 +123,13 @@ inline bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+inline bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 /// Diagnose mutually exclusive attributes when present on a given
 /// declaration. Returns true if diagnosed.
 template 
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 75a80540dbcbf..708c9a9288905 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -4602,6 +4602,10 @@ class Sema final : public SemaBase {
 
   enum class RetainOwnershipKind { NS, CF, OS };
 
+  void DiagnoseMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+  std::vector
+  GetMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index aa44608035538..d64c418ea40de 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -15921,6 +15921,8 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt 
*Body,
 }
   }
 
+  DiagnoseMissingFormatAttributes(Body, FD);
+
   // We might not have found a prototype because we didn't wish to warn on
   // the lack of a missing prototype. Try again without the checks for
   // whether we want to warn on the missing prototype.
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-09 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From a720cb0c8f0d1ca0f9d98385748e50e85c615612 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Attr.h   |   7 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaDecl.cpp   |   2 +
 clang/lib/Sema/SemaDeclAttr.cpp   | 215 +-
 clang/test/Sema/attr-format-missing.c | 390 ++
 clang/test/Sema/attr-format-missing.cpp   | 174 
 9 files changed, 796 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e3ed18e1b288a..b5201d9f0a5ad 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -665,6 +665,9 @@ Improvements to Clang's diagnostics
 
 - Clang now shows implicit deduction guides when diagnosing overload 
resolution failure. #GH92393.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH60718
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 2241f8481484e..da6a3b2fe3571 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -525,7 +525,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def MissingIncludeDirs : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1aba8bc24ba2f..955210e8d74b7 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1028,6 +1028,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Attr.h b/clang/include/clang/Sema/Attr.h
index 3f0b10212789a..37c124ca7b454 100644
--- a/clang/include/clang/Sema/Attr.h
+++ b/clang/include/clang/Sema/Attr.h
@@ -123,6 +123,13 @@ inline bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+inline bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 /// Diagnose mutually exclusive attributes when present on a given
 /// declaration. Returns true if diagnosed.
 template 
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 75a80540dbcbf..708c9a9288905 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -4602,6 +4602,10 @@ class Sema final : public SemaBase {
 
   enum class RetainOwnershipKind { NS, CF, OS };
 
+  void DiagnoseMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+  std::vector
+  GetMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index aa44608035538..d64c418ea40de 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -15921,6 +15921,8 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt 
*Body,
 }
   }
 
+  DiagnoseMissingFormatAttributes(Body, FD);
+
   // We might not have found a prototype because we didn't wish to warn on
   // the lack of a missing prototype. Try again without the checks for
   // whether we want to warn on the missing prototype.
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-07-09 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From a67db17140dca5f0b7e4099c08f2753eda7dd4f2 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Attr.h   |   7 +
 clang/include/clang/Sema/Sema.h   |   3 +
 clang/lib/Sema/SemaDecl.cpp   |   2 +
 clang/lib/Sema/SemaDeclAttr.cpp   | 192 -
 clang/test/Sema/attr-format-missing.c | 390 ++
 clang/test/Sema/attr-format-missing.cpp   | 174 
 9 files changed, 772 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9ed3ff4507671..63be1b80b983a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -626,6 +626,9 @@ Improvements to Clang's diagnostics
   used rather than when they are needed for constant evaluation or when code 
is generated for them.
   The check is now stricter to prevent crashes for some unsupported 
declarations (Fixes #GH95495).
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH60718
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 1c4f305fb5d00..4836b499fbbee 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def MissingIncludeDirs : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 46ad359751d7d..e4fd8904b9f4d 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1021,6 +1021,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Attr.h b/clang/include/clang/Sema/Attr.h
index 3f0b10212789a..37c124ca7b454 100644
--- a/clang/include/clang/Sema/Attr.h
+++ b/clang/include/clang/Sema/Attr.h
@@ -123,6 +123,13 @@ inline bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+inline bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 /// Diagnose mutually exclusive attributes when present on a given
 /// declaration. Returns true if diagnosed.
 template 
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 2e7af0f691cbb..80f7c27e33dfd 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3854,6 +3854,9 @@ class Sema final : public SemaBase {
 
   enum class RetainOwnershipKind { NS, CF, OS };
 
+  void DiagnoseMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+  std::vector GetMissingFormatAttributes(Stmt *Body, const 
FunctionDecl *FDecl);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 029ccf944c513..580ae510e384f 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -16253,6 +16253,8 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt 
*Body,
 }
   }
 
+  DiagnoseMissingFormatAttributes(Body, FD);
+
   // We might not have found a prototype because we didn't wish to warn on
   // the lack of a missing prototype. Try again without the checks for
   // whether we want to warn on the missing prototype.

[clang] [clang] Catch missing format attributes (PR #70024)

2024-06-27 Thread Aaron Ballman via cfe-commits


@@ -0,0 +1,277 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+
+typedef unsigned short char16_t;
+typedef unsigned int char32_t;
+typedef __WCHAR_TYPE__ wchar_t;
+typedef __SIZE_TYPE__ size_t;
+typedef __builtin_va_list va_list;
+
+__attribute__((__format__(__printf__, 1, 2)))
+int printf(const char *, ...); // #printf
+
+__attribute__((__format__(__scanf__, 1, 2)))
+int scanf(const char *, ...); // #scanf
+
+__attribute__((__format__(__printf__, 1, 0)))
+int vprintf(const char *, va_list); // #vprintf
+
+__attribute__((__format__(__scanf__, 1, 0)))
+int vscanf(const char *, va_list); // #vscanf
+
+__attribute__((__format__(__printf__, 2, 0)))
+int vsprintf(char *, const char *, va_list); // #vsprintf
+
+__attribute__((__format__(__printf__, 3, 0)))
+int vsnprintf(char *ch, size_t, const char *, va_list); // #vsnprintf
+
+__attribute__((__format__(__scanf__, 1, 4)))
+void f1(char *out, const size_t len, const char *format, ... /* args */) // #f1
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning@#f1 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f1'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 3, 4)))
+}
+
+__attribute__((__format__(__printf__, 1, 4)))
+void f2(char *out, const size_t len, const char *format, ... /* args */) // #f2
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning@#f2 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f2'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 3, 4)))
+}
+
+void f3(char *out, va_list args) // #f3
+{
+vprintf(out, args); // expected-warning@#f3 {{diagnostic behavior may be 
improved by adding the 'printf' format attribute to the declaration of 'f3'}}
+// CHECK-FIXES: __attribute__((format(printf, 1, 0)))
+vscanf(out, args); // expected-warning@#f3 {{diagnostic behavior may be 
improved by adding the 'scanf' format attribute to the declaration of 'f3'}}
+   // CHECK-FIXES: __attribute__((format(scanf, 1, 0)))

AaronBallman wrote:

Oops, sorry that I missed your question!

> My view of this comment is next: it refers to function which calls both 
> vprintf and vscanf format functions (let's call this function fn).

Correct -- that's what I meant; a function that uses both printing and scanning 
functions. Concretely:
```
#include 

void foo(const char *out, ...) {
va_list args;
va_start(args);
vscanf(out, args);
vprintf(out, args);
va_end(args)
}
```
there's no way for someone to call `foo("%d", ???)` that would work because the 
`???` would be of the wrong type for one of the formatted io calls, so I don't 
think such a function should have a suggestion to add both attributes (the way 
GCC does).

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-06-25 Thread Budimir Aranđelović via cfe-commits


@@ -0,0 +1,277 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+
+typedef unsigned short char16_t;
+typedef unsigned int char32_t;
+typedef __WCHAR_TYPE__ wchar_t;
+typedef __SIZE_TYPE__ size_t;
+typedef __builtin_va_list va_list;
+
+__attribute__((__format__(__printf__, 1, 2)))
+int printf(const char *, ...); // #printf
+
+__attribute__((__format__(__scanf__, 1, 2)))
+int scanf(const char *, ...); // #scanf
+
+__attribute__((__format__(__printf__, 1, 0)))
+int vprintf(const char *, va_list); // #vprintf
+
+__attribute__((__format__(__scanf__, 1, 0)))
+int vscanf(const char *, va_list); // #vscanf
+
+__attribute__((__format__(__printf__, 2, 0)))
+int vsprintf(char *, const char *, va_list); // #vsprintf
+
+__attribute__((__format__(__printf__, 3, 0)))
+int vsnprintf(char *ch, size_t, const char *, va_list); // #vsnprintf
+
+__attribute__((__format__(__scanf__, 1, 4)))
+void f1(char *out, const size_t len, const char *format, ... /* args */) // #f1
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning@#f1 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f1'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 3, 4)))
+}
+
+__attribute__((__format__(__printf__, 1, 4)))
+void f2(char *out, const size_t len, const char *format, ... /* args */) // #f2
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning@#f2 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f2'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 3, 4)))
+}
+
+void f3(char *out, va_list args) // #f3
+{
+vprintf(out, args); // expected-warning@#f3 {{diagnostic behavior may be 
improved by adding the 'printf' format attribute to the declaration of 'f3'}}
+// CHECK-FIXES: __attribute__((format(printf, 1, 0)))
+vscanf(out, args); // expected-warning@#f3 {{diagnostic behavior may be 
improved by adding the 'scanf' format attribute to the declaration of 'f3'}}
+   // CHECK-FIXES: __attribute__((format(scanf, 1, 0)))

budimirarandjelovicsyrmia wrote:

ping @AaronBallman
My view of this comment is next: it refers to function which calls both vprintf 
and vscanf format functions (let's call this function _fn_). As same format 
specifiers of calling functions mean different things and these two functions 
are called inside same function, there is pretty small chance of importance to 
mark function _fn_ to be format function.
Is this view correct?

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-06-25 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 5bc76cd96f52cf8184712c8e1ffa521f05e3e29c Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Attr.h   |   7 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 112 ++-
 clang/test/Sema/attr-format-missing.c | 304 ++
 clang/test/Sema/attr-format-missing.cpp   | 178 ++
 9 files changed, 612 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9ed3ff4507671..63be1b80b983a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -626,6 +626,9 @@ Improvements to Clang's diagnostics
   used rather than when they are needed for constant evaluation or when code 
is generated for them.
   The check is now stricter to prevent crashes for some unsupported 
declarations (Fixes #GH95495).
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH60718
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 1c4f305fb5d00..4836b499fbbee 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def MissingIncludeDirs : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 46ad359751d7d..e4fd8904b9f4d 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1021,6 +1021,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Attr.h b/clang/include/clang/Sema/Attr.h
index 3f0b10212789a..37c124ca7b454 100644
--- a/clang/include/clang/Sema/Attr.h
+++ b/clang/include/clang/Sema/Attr.h
@@ -123,6 +123,13 @@ inline bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+inline bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 /// Diagnose mutually exclusive attributes when present on a given
 /// declaration. Returns true if diagnosed.
 template 
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 2e7af0f691cbb..74b6a8841da4a 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3854,6 +3854,10 @@ class Sema final : public SemaBase {
 
   enum class RetainOwnershipKind { NS, CF, OS };
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 87988519e7691..63697fab64ee1 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -4119,8 +4119,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 void Sema::CheckConstrainedAuto(const AutoType *AutoT, 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-06-24 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 97cc6f1e97bb27422df8fa0ecce2f4ad1dfcacff Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Attr.h   |   7 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 112 ++-
 clang/test/Sema/attr-format-missing.c | 304 ++
 clang/test/Sema/attr-format-missing.cpp   | 178 ++
 9 files changed, 612 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9c8f8c4a4fbaf..31e31ab4ac262 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -612,6 +612,9 @@ Improvements to Clang's diagnostics
   used rather than when they are needed for constant evaluation or when code 
is generated for them.
   The check is now stricter to prevent crashes for some unsupported 
declarations (Fixes #GH95495).
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH60718
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 9b37d4bd3205b..4e075eb7c062a 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def MissingIncludeDirs : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 25a87078a5709..f92aa7be8effb 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1021,6 +1021,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Attr.h b/clang/include/clang/Sema/Attr.h
index 3f0b10212789a..37c124ca7b454 100644
--- a/clang/include/clang/Sema/Attr.h
+++ b/clang/include/clang/Sema/Attr.h
@@ -123,6 +123,13 @@ inline bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+inline bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 /// Diagnose mutually exclusive attributes when present on a given
 /// declaration. Returns true if diagnosed.
 template 
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index e43e5f465361d..93906437c6626 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3775,6 +3775,10 @@ class Sema final : public SemaBase {
 
   enum class RetainOwnershipKind { NS, CF, OS };
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 87988519e7691..63697fab64ee1 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -4119,8 +4119,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 void Sema::CheckConstrainedAuto(const AutoType *AutoT, 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-06-13 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From f87e413ff9dcaa2e5ab73a565f168cc55dc54db0 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Attr.h   |   7 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 112 ++-
 clang/test/Sema/attr-format-missing.c | 304 ++
 clang/test/Sema/attr-format-missing.cpp   | 178 ++
 9 files changed, 612 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index cf1ba02cbc4b2..6ffd22cf97c30 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -569,6 +569,9 @@ Improvements to Clang's diagnostics
 - Clang no longer emits a "declared here" note for a builtin function that has 
no declaration in source.
   Fixes #GH93369.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH60718
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 9b37d4bd3205b..4e075eb7c062a 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def MissingIncludeDirs : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index a104dfb766a98..9ef6e8d6a877f 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1012,6 +1012,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Attr.h b/clang/include/clang/Sema/Attr.h
index 3f0b10212789a..37c124ca7b454 100644
--- a/clang/include/clang/Sema/Attr.h
+++ b/clang/include/clang/Sema/Attr.h
@@ -123,6 +123,13 @@ inline bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+inline bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 /// Diagnose mutually exclusive attributes when present on a given
 /// declaration. Returns true if diagnosed.
 template 
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 9bf01417186c3..98bc520d3dc51 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3775,6 +3775,10 @@ class Sema final : public SemaBase {
 
   enum class RetainOwnershipKind { NS, CF, OS };
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 07cd0727eb3f4..20303267c2d5d 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -3994,8 +3994,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 void Sema::CheckConstrainedAuto(const AutoType *AutoT, SourceLocation Loc) {
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-06-12 Thread Budimir Aranđelović via cfe-commits


@@ -0,0 +1,277 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+
+typedef unsigned short char16_t;
+typedef unsigned int char32_t;
+typedef __WCHAR_TYPE__ wchar_t;
+typedef __SIZE_TYPE__ size_t;
+typedef __builtin_va_list va_list;
+
+__attribute__((__format__(__printf__, 1, 2)))
+int printf(const char *, ...); // #printf
+
+__attribute__((__format__(__scanf__, 1, 2)))
+int scanf(const char *, ...); // #scanf
+
+__attribute__((__format__(__printf__, 1, 0)))
+int vprintf(const char *, va_list); // #vprintf
+
+__attribute__((__format__(__scanf__, 1, 0)))
+int vscanf(const char *, va_list); // #vscanf
+
+__attribute__((__format__(__printf__, 2, 0)))
+int vsprintf(char *, const char *, va_list); // #vsprintf
+
+__attribute__((__format__(__printf__, 3, 0)))
+int vsnprintf(char *ch, size_t, const char *, va_list); // #vsnprintf
+
+__attribute__((__format__(__scanf__, 1, 4)))
+void f1(char *out, const size_t len, const char *format, ... /* args */) // #f1
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning@#f1 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f1'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 3, 4)))
+}
+
+__attribute__((__format__(__printf__, 1, 4)))
+void f2(char *out, const size_t len, const char *format, ... /* args */) // #f2
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning@#f2 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f2'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 3, 4)))
+}
+
+void f3(char *out, va_list args) // #f3
+{
+vprintf(out, args); // expected-warning@#f3 {{diagnostic behavior may be 
improved by adding the 'printf' format attribute to the declaration of 'f3'}}
+// CHECK-FIXES: __attribute__((format(printf, 1, 0)))
+vscanf(out, args); // expected-warning@#f3 {{diagnostic behavior may be 
improved by adding the 'scanf' format attribute to the declaration of 'f3'}}
+   // CHECK-FIXES: __attribute__((format(scanf, 1, 0)))

budimirarandjelovicsyrmia wrote:

I don't understand this comment. Can you explain in details?

Which function you point? Is this suggestion refers only to these functions or 
general?

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-06-12 Thread Budimir Aranđelović via cfe-commits


@@ -0,0 +1,303 @@
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,beforeCxx2b 
-Wmissing-format-attribute %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2b -Wmissing-format-attribute 
%s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++23 -Wmissing-format-attribute 
%s
+
+typedef __SIZE_TYPE__ size_t;
+typedef __builtin_va_list va_list;
+
+namespace std
+{
+template struct basic_string_view {};
+template struct basic_string {
+const Elem *c_str() const noexcept;
+basic_string(const basic_string_view SW);
+};
+
+using string = basic_string;
+using wstring = basic_string;
+using string_view = basic_string_view;
+using wstring_view = basic_string_view;
+}
+
+__attribute__((__format__(__printf__, 1, 2)))
+int printf(const char *, ...); // #printf
+
+__attribute__((__format__(__scanf__, 1, 2)))
+int scanf(const char *, ...); // #scanf
+
+__attribute__((__format__(__printf__, 1, 0)))
+int vprintf(const char *, va_list); // #vprintf
+
+__attribute__((__format__(__scanf__, 1, 0)))
+int vscanf(const char *, va_list); // #vscanf
+
+__attribute__((__format__(__printf__, 2, 0)))
+int vsprintf(char *, const char *, va_list); // #vsprintf
+
+__attribute__((__format__(__printf__, 3, 0)))
+int vsnprintf(char *ch, size_t, const char *, va_list); // #vsnprintf
+
+void f1(const std::string , ... /* args */) // #f1
+{
+va_list args;
+vscanf(str.c_str(), args); // no warning
+vprintf(str.c_str(), args); // no warning
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f2(const std::string , ... /* args */); // #f2
+
+void f3(std::string_view str, ... /* args */) // #f3
+{
+va_list args;
+vscanf(std::string(str).c_str(), args); // no warning
+vprintf(std::string(str).c_str(), args); // no warning
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f4(std::string_view str, ... /* args */); // #f4
+
+void f5(const std::wstring , ... /* args */) // #f5
+{
+va_list args;
+vscanf((const char *)str.c_str(), args); // no warning
+vprintf((const char *)str.c_str(), args); // no warning
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f6(const std::wstring , ... /* args */); // #f6
+
+void f7(std::wstring_view str, ... /* args */) // #f7
+{
+va_list args;
+vscanf((const char *) std::wstring(str).c_str(), args); // no warning
+vprintf((const char *) std::wstring(str).c_str(), args); // no warning
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f8(std::wstring_view str, ... /* args */); // #f8
+
+void f9(const wchar_t *out, ... /* args */) // #f9
+{
+va_list args;
+vprintf(out, args); // expected-error {{no matching function for call to 
'vprintf'}}
+// expected-note@#vprintf {{candidate function not 
viable: no known conversion from 'const wchar_t *' to 'const char *' for 1st 
argument}}
+vscanf((const char *) out, args); // expected-warning@#f9 {{diagnostic 
behavior may be improved by adding the 'scanf' format attribute to the 
declaration of 'f9'}}
+  // CHECK-FIXES: 
__attribute__((format(scanf, 1, 2)))
+vscanf((char *) out, args); // expected-warning@#f9 {{diagnostic behavior 
may be improved by adding the 'scanf' format attribute to the declaration of 
'f9'}}
+// CHECK-FIXES: __attribute__((format(scanf, 
1, 2)))
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f10(const wchar_t *out, ... /* args */); // #f10
+
+void f11(const char16_t *out, ... /* args */) // #f11

budimirarandjelovicsyrmia wrote:

It makes sense. I agree.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-06-12 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From f751c9f2aa48d80cd7facca289317fc306068f38 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 121 ++-
 clang/test/Sema/attr-format-missing.c | 304 ++
 clang/test/Sema/attr-format-missing.cpp   | 178 ++
 8 files changed, 613 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 594e053af02da..e8020d6aee1a3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -546,6 +546,9 @@ Improvements to Clang's diagnostics
 - Clang no longer emits a "declared here" note for a builtin function that has 
no declaration in source.
   Fixes #GH93369.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH60718
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 7d5ba7869ec34..5d3e9314a29da 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index e34eb692941b4..c161474799412 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1010,6 +1010,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 7dea2b6826cfd..5164b8e96ae56 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3757,6 +3757,10 @@ class Sema final : public SemaBase {
   bool DiagnoseSwiftName(Decl *D, StringRef Name, SourceLocation Loc,
  const ParsedAttr , bool IsAsync);
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 300af02239779..2962f44455d0f 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -3994,8 +3994,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 void Sema::CheckConstrainedAuto(const AutoType *AutoT, SourceLocation Loc) {
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 7c1fb23b90728..47d9ba2444177 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -156,6 +156,13 @@ static bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+static bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 static inline bool isNSStringType(QualType T, ASTContext ,
   bool AllowNSAttributedString = false) {
   const auto *PT = T->getAs();
@@ -260,7 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-31 Thread Aaron Ballman via cfe-commits


@@ -501,6 +501,9 @@ Improvements to Clang's diagnostics
 - Clang emits a ``-Wparentheses`` warning for expressions with consecutive 
comparisons like ``x < y < z``.
   Fixes #GH20456.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH70024

AaronBallman wrote:

```suggestion
  class/struct/union members. Fixes #GH60718
```


https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-31 Thread Aaron Ballman via cfe-commits


@@ -0,0 +1,277 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+
+typedef unsigned short char16_t;
+typedef unsigned int char32_t;
+typedef __WCHAR_TYPE__ wchar_t;
+typedef __SIZE_TYPE__ size_t;
+typedef __builtin_va_list va_list;
+
+__attribute__((__format__(__printf__, 1, 2)))
+int printf(const char *, ...); // #printf
+
+__attribute__((__format__(__scanf__, 1, 2)))
+int scanf(const char *, ...); // #scanf
+
+__attribute__((__format__(__printf__, 1, 0)))
+int vprintf(const char *, va_list); // #vprintf
+
+__attribute__((__format__(__scanf__, 1, 0)))
+int vscanf(const char *, va_list); // #vscanf
+
+__attribute__((__format__(__printf__, 2, 0)))
+int vsprintf(char *, const char *, va_list); // #vsprintf
+
+__attribute__((__format__(__printf__, 3, 0)))
+int vsnprintf(char *ch, size_t, const char *, va_list); // #vsnprintf
+
+__attribute__((__format__(__scanf__, 1, 4)))
+void f1(char *out, const size_t len, const char *format, ... /* args */) // #f1
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning@#f1 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f1'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 3, 4)))
+}
+
+__attribute__((__format__(__printf__, 1, 4)))
+void f2(char *out, const size_t len, const char *format, ... /* args */) // #f2
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning@#f2 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f2'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 3, 4)))
+}
+
+void f3(char *out, va_list args) // #f3
+{
+vprintf(out, args); // expected-warning@#f3 {{diagnostic behavior may be 
improved by adding the 'printf' format attribute to the declaration of 'f3'}}
+// CHECK-FIXES: __attribute__((format(printf, 1, 0)))
+vscanf(out, args); // expected-warning@#f3 {{diagnostic behavior may be 
improved by adding the 'scanf' format attribute to the declaration of 'f3'}}
+   // CHECK-FIXES: __attribute__((format(scanf, 1, 0)))

AaronBallman wrote:

This seems like a situation where it makes more sense to not emit the 
diagnostic or the fix-it, right? The chances of this function being a format 
function in practice are pretty small because the format specifiers mean 
different things. e.g., `%d` is a specifier for an `int` for `printf` but `int 
*` for `scanf`.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-31 Thread Aaron Ballman via cfe-commits


@@ -7131,6 +7138,114 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, 
const ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// This function is called only if function call is not inside template body.
+// TODO: Add call for function calls inside template body.
+// Check if parent function misses format attribute. If misses, emit warning.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc) {
+  assert(FDecl);
+
+  const FunctionDecl *ParentFuncDecl = getCurFunctionDecl();
+  if (!ParentFuncDecl)
+return;
+
+  // If function is a member of struct/union/class and has implicit object
+  // parameter, format attribute argument indexing starts from 2. Otherwise, it
+  // starts from 1.
+  unsigned int FormatArgumentIndexOffset =
+  checkIfMethodHasImplicitObjectParameter(FDecl) ? 2 : 1;
+  unsigned int ParentFunctionFormatArgumentIndexOffset =
+  checkIfMethodHasImplicitObjectParameter(ParentFuncDecl) ? 2 : 1;
+
+  // Check if function has format attribute with forwarded format string.
+  IdentifierInfo *AttrType;
+  const ParmVarDecl *FormatArg;
+  if (!llvm::any_of(FDecl->specific_attrs(),
+[&](const FormatAttr *Attr) {
+  const int FormatIndexOffseted =

AaronBallman wrote:

```suggestion
  int OffsetFormatIndex =
```

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-31 Thread Aaron Ballman via cfe-commits


@@ -0,0 +1,277 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+
+typedef unsigned short char16_t;
+typedef unsigned int char32_t;
+typedef __WCHAR_TYPE__ wchar_t;
+typedef __SIZE_TYPE__ size_t;
+typedef __builtin_va_list va_list;
+
+__attribute__((__format__(__printf__, 1, 2)))
+int printf(const char *, ...); // #printf
+
+__attribute__((__format__(__scanf__, 1, 2)))
+int scanf(const char *, ...); // #scanf
+
+__attribute__((__format__(__printf__, 1, 0)))
+int vprintf(const char *, va_list); // #vprintf
+
+__attribute__((__format__(__scanf__, 1, 0)))
+int vscanf(const char *, va_list); // #vscanf
+
+__attribute__((__format__(__printf__, 2, 0)))
+int vsprintf(char *, const char *, va_list); // #vsprintf
+
+__attribute__((__format__(__printf__, 3, 0)))
+int vsnprintf(char *ch, size_t, const char *, va_list); // #vsnprintf
+
+__attribute__((__format__(__scanf__, 1, 4)))
+void f1(char *out, const size_t len, const char *format, ... /* args */) // #f1
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning@#f1 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f1'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 3, 4)))

AaronBallman wrote:

This is not testing anything; that's a magic comment for the static analyzer 
cc1 command. You'll need to use `-fdiagnostics-parseable-fixits` and 
`FileCheck` to check fixes in general (you can search for that command line 
option and model after one of the existing tests).

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-31 Thread Aaron Ballman via cfe-commits


@@ -0,0 +1,303 @@
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,beforeCxx2b 
-Wmissing-format-attribute %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2b -Wmissing-format-attribute 
%s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++23 -Wmissing-format-attribute 
%s
+
+typedef __SIZE_TYPE__ size_t;
+typedef __builtin_va_list va_list;
+
+namespace std
+{
+template struct basic_string_view {};
+template struct basic_string {
+const Elem *c_str() const noexcept;
+basic_string(const basic_string_view SW);
+};
+
+using string = basic_string;
+using wstring = basic_string;
+using string_view = basic_string_view;
+using wstring_view = basic_string_view;
+}
+
+__attribute__((__format__(__printf__, 1, 2)))
+int printf(const char *, ...); // #printf
+
+__attribute__((__format__(__scanf__, 1, 2)))
+int scanf(const char *, ...); // #scanf
+
+__attribute__((__format__(__printf__, 1, 0)))
+int vprintf(const char *, va_list); // #vprintf
+
+__attribute__((__format__(__scanf__, 1, 0)))
+int vscanf(const char *, va_list); // #vscanf
+
+__attribute__((__format__(__printf__, 2, 0)))
+int vsprintf(char *, const char *, va_list); // #vsprintf
+
+__attribute__((__format__(__printf__, 3, 0)))
+int vsnprintf(char *ch, size_t, const char *, va_list); // #vsnprintf
+
+void f1(const std::string , ... /* args */) // #f1
+{
+va_list args;
+vscanf(str.c_str(), args); // no warning
+vprintf(str.c_str(), args); // no warning
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f2(const std::string , ... /* args */); // #f2
+
+void f3(std::string_view str, ... /* args */) // #f3
+{
+va_list args;
+vscanf(std::string(str).c_str(), args); // no warning
+vprintf(std::string(str).c_str(), args); // no warning
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f4(std::string_view str, ... /* args */); // #f4
+
+void f5(const std::wstring , ... /* args */) // #f5
+{
+va_list args;
+vscanf((const char *)str.c_str(), args); // no warning
+vprintf((const char *)str.c_str(), args); // no warning
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f6(const std::wstring , ... /* args */); // #f6
+
+void f7(std::wstring_view str, ... /* args */) // #f7
+{
+va_list args;
+vscanf((const char *) std::wstring(str).c_str(), args); // no warning
+vprintf((const char *) std::wstring(str).c_str(), args); // no warning
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f8(std::wstring_view str, ... /* args */); // #f8
+
+void f9(const wchar_t *out, ... /* args */) // #f9
+{
+va_list args;
+vprintf(out, args); // expected-error {{no matching function for call to 
'vprintf'}}
+// expected-note@#vprintf {{candidate function not 
viable: no known conversion from 'const wchar_t *' to 'const char *' for 1st 
argument}}
+vscanf((const char *) out, args); // expected-warning@#f9 {{diagnostic 
behavior may be improved by adding the 'scanf' format attribute to the 
declaration of 'f9'}}
+  // CHECK-FIXES: 
__attribute__((format(scanf, 1, 2)))
+vscanf((char *) out, args); // expected-warning@#f9 {{diagnostic behavior 
may be improved by adding the 'scanf' format attribute to the declaration of 
'f9'}}
+// CHECK-FIXES: __attribute__((format(scanf, 
1, 2)))
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f10(const wchar_t *out, ... /* args */); // #f10
+
+void f11(const char16_t *out, ... /* args */) // #f11

AaronBallman wrote:

This doesn't seem like a particularly helpful test because it's catching a type 
mismatch rather than anything with format attributes.

That said, I think functions that can be either valid C or C++ should be kept 
in the .c file and that file can get a C++-specific `RUN` line to test those 
situations. Then this .cpp file can be focused on constructs that exist in C++ 
but not C. WDYT?

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-31 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 33f9a9b3df756331625ea3cc081e7ee7443d03f5 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 121 ++-
 clang/test/Sema/attr-format-missing.c | 277 
 clang/test/Sema/attr-format-missing.cpp   | 303 ++
 8 files changed, 711 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 594e053af02da..b0e1e6766a709 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -546,6 +546,9 @@ Improvements to Clang's diagnostics
 - Clang no longer emits a "declared here" note for a builtin function that has 
no declaration in source.
   Fixes #GH93369.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH70024
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 7d5ba7869ec34..5d3e9314a29da 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index e34eb692941b4..c161474799412 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1010,6 +1010,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 7dea2b6826cfd..5164b8e96ae56 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3757,6 +3757,10 @@ class Sema final : public SemaBase {
   bool DiagnoseSwiftName(Decl *D, StringRef Name, SourceLocation Loc,
  const ParsedAttr , bool IsAsync);
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 300af02239779..2962f44455d0f 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -3994,8 +3994,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 void Sema::CheckConstrainedAuto(const AutoType *AutoT, SourceLocation Loc) {
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 7c1fb23b90728..e385aedd58c6c 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -156,6 +156,13 @@ static bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+static bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 static inline bool isNSStringType(QualType T, ASTContext ,
   bool AllowNSAttributedString = false) {
   const auto *PT = T->getAs();
@@ -260,7 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-31 Thread Aaron Ballman via cfe-commits

https://github.com/AaronBallman edited 
https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-31 Thread Budimir Aranđelović via cfe-commits

budimirarandjelovicsyrmia wrote:

Ping @AaronBallman @aaronpuchert Any comment? (except Resolve conflicts)

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-23 Thread Vlad Serebrennikov via cfe-commits

https://github.com/Endilll commented:

Sema.h changes look good.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-23 Thread Budimir Aranđelović via cfe-commits

budimirarandjelovicsyrmia wrote:

ping @AaronBallman @Endilll @aaronpuchert

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-22 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 44e3593c8409a49d878d6a0b2eb97732c01cd7b3 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 121 ++-
 clang/test/Sema/attr-format-missing.c | 277 
 clang/test/Sema/attr-format-missing.cpp   | 303 ++
 8 files changed, 711 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 5a123b0b86dda..b6851374fdccc 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -501,6 +501,9 @@ Improvements to Clang's diagnostics
 - Clang emits a ``-Wparentheses`` warning for expressions with consecutive 
comparisons like ``x < y < z``.
   Fixes #GH20456.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH70024
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 4cb4f3d999f7a..1bd826eb715f9 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -507,7 +507,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index e3b4186f1b06f..493791854a856 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1010,6 +1010,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 6c89d275215de..f149419ac6a66 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3688,6 +3688,10 @@ class Sema final : public SemaBase {
   bool DiagnoseSwiftName(Decl *D, StringRef Name, SourceLocation Loc,
  const ParsedAttr , bool IsAsync);
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index f2dc8e9dd0050..0277375a7600a 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8088,8 +8088,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 void Sema::CheckConstrainedAuto(const AutoType *AutoT, SourceLocation Loc) {
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index ca5938083917f..57c301a7e0eef 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -166,6 +166,13 @@ static bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+static bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 static inline bool isNSStringType(QualType T, ASTContext ,
   bool AllowNSAttributedString = false) {
   const auto *PT = T->getAs();
@@ 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-22 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 0fc447dabd674597bbff456f638ee52a49ef5b0c Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 120 ++-
 clang/test/Sema/attr-format-missing.c | 277 
 clang/test/Sema/attr-format-missing.cpp   | 303 ++
 8 files changed, 710 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 5a123b0b86dda..b6851374fdccc 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -501,6 +501,9 @@ Improvements to Clang's diagnostics
 - Clang emits a ``-Wparentheses`` warning for expressions with consecutive 
comparisons like ``x < y < z``.
   Fixes #GH20456.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH70024
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 4cb4f3d999f7a..1bd826eb715f9 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -507,7 +507,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index e3b4186f1b06f..493791854a856 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1010,6 +1010,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 6c89d275215de..f149419ac6a66 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3688,6 +3688,10 @@ class Sema final : public SemaBase {
   bool DiagnoseSwiftName(Decl *D, StringRef Name, SourceLocation Loc,
  const ParsedAttr , bool IsAsync);
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index f2dc8e9dd0050..0277375a7600a 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8088,8 +8088,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 void Sema::CheckConstrainedAuto(const AutoType *AutoT, SourceLocation Loc) {
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index ca5938083917f..24bb69d66c162 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -166,6 +166,13 @@ static bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+static bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 static inline bool isNSStringType(QualType T, ASTContext ,
   bool AllowNSAttributedString = false) {
   const auto *PT = T->getAs();
@@ 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-20 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 9b9177b9b6e42fdf1cd54ccd9f16aae892b59f28 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 120 ++-
 clang/test/Sema/attr-format-missing.c | 277 
 clang/test/Sema/attr-format-missing.cpp   | 303 ++
 8 files changed, 710 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 5a123b0b86dda..b6851374fdccc 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -501,6 +501,9 @@ Improvements to Clang's diagnostics
 - Clang emits a ``-Wparentheses`` warning for expressions with consecutive 
comparisons like ``x < y < z``.
   Fixes #GH20456.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH70024
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 4cb4f3d999f7a..1bd826eb715f9 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -507,7 +507,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index e3b4186f1b06f..493791854a856 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1010,6 +1010,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 6c89d275215de..f149419ac6a66 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3688,6 +3688,10 @@ class Sema final : public SemaBase {
   bool DiagnoseSwiftName(Decl *D, StringRef Name, SourceLocation Loc,
  const ParsedAttr , bool IsAsync);
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index f2dc8e9dd0050..0277375a7600a 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8088,8 +8088,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 void Sema::CheckConstrainedAuto(const AutoType *AutoT, SourceLocation Loc) {
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index ca5938083917f..c978182a625ea 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -166,6 +166,13 @@ static bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+static bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 static inline bool isNSStringType(QualType T, ASTContext ,
   bool AllowNSAttributedString = false) {
   const auto *PT = T->getAs();
@@ 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-10 Thread Budimir Aranđelović via cfe-commits

budimirarandjelovicsyrmia wrote:

ping

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-10 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 024e4f0d0de64ba1f5f68eea578eb4ac1a554f0e Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 120 ++-
 clang/test/Sema/attr-format-missing.c | 277 
 clang/test/Sema/attr-format-missing.cpp   | 303 ++
 8 files changed, 710 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a85095e424b64..a07b223b48509 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -473,6 +473,9 @@ Improvements to Clang's diagnostics
}
  };
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH70024
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 60f87da2a7387..5fa2f6e7db4fa 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 9a0bae9c216de..1fbe379d9af0b 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1010,6 +1010,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index a80ac6dbc7613..5552a48456cde 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3725,6 +3725,10 @@ class Sema final : public SemaBase {
   bool DiagnoseSwiftName(Decl *D, StringRef Name, SourceLocation Loc,
  const ParsedAttr , bool IsAsync);
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3179d542b1f92..fd5de633be318 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8085,8 +8085,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 363ae93cb62df..93c9d734d2533 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -163,6 +163,13 @@ static bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+static bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 static inline bool isNSStringType(QualType T, ASTContext ,
   bool AllowNSAttributedString = false) {
   const auto *PT = T->getAs();
@@ -312,7 +319,7 @@ static bool checkFunctionOrMethodParameterIndex(
   // In C++ the implicit 'this' function 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-05-07 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 75ec541cdf717931afe70abc412cb1747f84e0d7 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 119 ++-
 clang/test/Sema/attr-format-missing.c | 277 
 clang/test/Sema/attr-format-missing.cpp   | 303 ++
 8 files changed, 709 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a85095e424b64..a07b223b48509 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -473,6 +473,9 @@ Improvements to Clang's diagnostics
}
  };
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH70024
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 60f87da2a7387..5fa2f6e7db4fa 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 9a0bae9c216de..1fbe379d9af0b 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1010,6 +1010,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index a80ac6dbc7613..5552a48456cde 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3725,6 +3725,10 @@ class Sema final : public SemaBase {
   bool DiagnoseSwiftName(Decl *D, StringRef Name, SourceLocation Loc,
  const ParsedAttr , bool IsAsync);
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3179d542b1f92..fd5de633be318 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8085,8 +8085,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 363ae93cb62df..5f5403d303a90 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -163,6 +163,13 @@ static bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+static bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 static inline bool isNSStringType(QualType T, ASTContext ,
   bool AllowNSAttributedString = false) {
   const auto *PT = T->getAs();
@@ -312,7 +319,7 @@ static bool checkFunctionOrMethodParameterIndex(
   // In C++ the implicit 'this' function 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-04-19 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 79873c374c982eed00c3adbc91b5cbff91c5946b Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 119 ++-
 clang/test/Sema/attr-format-missing.c | 272 
 clang/test/Sema/attr-format-missing.cpp   | 303 ++
 8 files changed, 704 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6c51c2d1f483ce..8cda4d62eabcbb 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -366,6 +366,9 @@ Improvements to Clang's diagnostics
 
 - Clang now diagnoses requires expressions with explicit object parameters.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH70024
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 5251774ff4efd6..0dadc5526344e1 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 30a8543489f48e..043214dc2400c8 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1008,6 +1008,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 1e89dfc58d92b1..82f93c572a2b29 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3724,6 +3724,10 @@ class Sema final : public SemaBase {
   bool DiagnoseSwiftName(Decl *D, StringRef Name, SourceLocation Loc,
  const ParsedAttr , bool IsAsync);
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 6778bc6607f75e..32f1b564247f32 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8040,8 +8040,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index c3bf18a3f79e23..89cb6bd15a8b4f 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -163,6 +163,13 @@ static bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+static bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 static inline bool isNSStringType(QualType T, ASTContext ,
   bool AllowNSAttributedString = false) {
   const auto *PT = T->getAs();
@@ -312,7 +319,7 @@ static bool 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-04-18 Thread Aaron Ballman via cfe-commits

AaronBallman wrote:

Pre-commit CI is crashing and it seems related to your changes:
```
[10077/10756] Generating 
ASAN_NOINST_TEST_OBJECTS.gtest-all.cc.x86_64-calls.o_bk;t=1713432335867

_bk;t=1713432335867FAILED: 
projects/compiler-rt/lib/asan/tests/ASAN_NOINST_TEST_OBJECTS.gtest-all.cc.x86_64-calls.o
 
/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/projects/compiler-rt/lib/asan/tests/ASAN_NOINST_TEST_OBJECTS.gtest-all.cc.x86_64-calls.o
 

_bk;t=1713432335867cd 
/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/projects/compiler-rt/lib/asan/tests
 && 
/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/./bin/clang
 -Wthread-safety -Wthread-safety-reference -Wthread-safety-beta 
-I/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/compiler-rt/include
 -g -Wno-covered-switch-default -Wno-suggest-override -DGTEST_NO_LLVM_SUPPORT=1 
-DGTEST_HAS_RTTI=0 
-I/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/llvm/../third-party/unittest/googletest/include
 
-I/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/llvm/../third-party/unittest/googletest
 
-I/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/compiler-rt/include
 
-I/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/compiler-rt/lib
 
-I/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/compiler-rt/lib/asan
 
-I/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/compiler-rt/lib/sanitizer_common/tests
 -DSANITIZER_COMMON_NO_REDEFINE_BUILTINS -fno-rtti -O2 -Wno-format 
-Werror=sign-compare -Wno-variadic-macros -gline-tables-only 
-DASAN_HAS_IGNORELIST=1 -DASAN_HAS_EXCEPTIONS=1 -DASAN_UAR=0 -m64 -c -o 
ASAN_NOINST_TEST_OBJECTS.gtest-all.cc.x86_64-calls.o 
/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/third-party/unittest/googletest/src/gtest-all.cc

_bk;t=1713432335867PLEASE submit a bug report to 
https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, 
preprocessed source, and associated run script.

_bk;t=1713432335867Stack dump:

_bk;t=17134323358670. Program arguments: 
/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/./bin/clang
 -Wthread-safety -Wthread-safety-reference -Wthread-safety-beta 
-I/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/compiler-rt/include
 -g -Wno-covered-switch-default -Wno-suggest-override -DGTEST_NO_LLVM_SUPPORT=1 
-DGTEST_HAS_RTTI=0 
-I/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/llvm/../third-party/unittest/googletest/include
 
-I/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/llvm/../third-party/unittest/googletest
 
-I/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/compiler-rt/include
 
-I/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/compiler-rt/lib
 
-I/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/compiler-rt/lib/asan
 
-I/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/compiler-rt/lib/sanitizer_common/tests
 -DSANITIZER_COMMON_NO_REDEFINE_BUILTINS -fno-rtti -O2 -Wno-format 
-Werror=sign-compare -Wno-variadic-macros -gline-tables-only 
-DASAN_HAS_IGNORELIST=1 -DASAN_HAS_EXCEPTIONS=1 -DASAN_UAR=0 -m64 -c -o 
ASAN_NOINST_TEST_OBJECTS.gtest-all.cc.x86_64-calls.o 
/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/third-party/unittest/googletest/src/gtest-all.cc

_bk;t=17134323358671. 
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/x86_64-linux-gnu/c++/11/bits/c++locale.h:92:71:
 current parser token ')'

_bk;t=17134323358672. 
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/x86_64-linux-gnu/c++/11/bits/c++locale.h:58:1:
 parsing namespace 'std'

_bk;t=17134323358673. 
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/x86_64-linux-gnu/c++/11/bits/c++locale.h:73:3:
 parsing function body 'std::__convert_from_v'

_bk;t=17134323358674. 
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/x86_64-linux-gnu/c++/11/bits/c++locale.h:73:3:
 in compound statement ('{}')

_bk;t=1713432335867 #0 0x561d75af1528 
llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) 
/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/llvm/lib/Support/Unix/Signals.inc:723:13

_bk;t=1713432335867 #1 0x561d75aef04e llvm::sys::RunSignalHandlers() 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-04-18 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 80274db3b96ec8dc4b19da9290d3a4748b679e07 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 119 ++-
 clang/test/Sema/attr-format-missing.c | 272 
 clang/test/Sema/attr-format-missing.cpp   | 303 ++
 8 files changed, 704 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6c51c2d1f483ce1..8cda4d62eabcbb0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -366,6 +366,9 @@ Improvements to Clang's diagnostics
 
 - Clang now diagnoses requires expressions with explicit object parameters.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH70024
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 5251774ff4efd6d..0dadc5526344e12 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 30a8543489f48ed..043214dc2400c82 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1008,6 +1008,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 1e89dfc58d92b17..82f93c572a2b293 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3724,6 +3724,10 @@ class Sema final : public SemaBase {
   bool DiagnoseSwiftName(Decl *D, StringRef Name, SourceLocation Loc,
  const ParsedAttr , bool IsAsync);
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 6778bc6607f75e1..32f1b564247f322 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8040,8 +8040,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index c3bf18a3f79e23e..682d798968504af 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -163,6 +163,13 @@ static bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+static bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 static inline bool isNSStringType(QualType T, ASTContext ,
   bool AllowNSAttributedString = false) {
   const auto *PT = T->getAs();
@@ -312,7 +319,7 @@ static 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-04-18 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 9286a2cc9acec86284e4b1a4b3d42e3116d46fa1 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 119 ++-
 clang/test/Sema/attr-format-missing.c | 272 
 clang/test/Sema/attr-format-missing.cpp   | 303 ++
 8 files changed, 704 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3a84ff16a1e4d4..f5a34fd9b8d24f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -328,6 +328,9 @@ Improvements to Clang's diagnostics
 - New ``-Wformat-signedness`` diagnostic that warn if the format string 
requires an
   unsigned argument and the argument is signed and vice versa.
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH70024
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 520168f01fd846..5c6a69ff1586db 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index df57f5e6ce11ba..d32bf4c8a9549e 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1008,6 +1008,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 8c98d8c7fef7a7..2bf2537027d728 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3885,6 +3885,10 @@ class Sema final {
   bool DiagnoseSwiftName(Decl *D, StringRef Name, SourceLocation Loc,
  const ParsedAttr , bool IsAsync);
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3dcd18b3afc8b4..77e01ba344e5a5 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8034,8 +8034,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 8bce04640e748e..fbf6f8f6f9534d 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -159,6 +159,13 @@ static bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+static bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 static inline bool isNSStringType(QualType T, ASTContext ,
   bool AllowNSAttributedString = false) {
   const auto *PT = 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-04-18 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From d74a1c4372fe343d8c7a0560e3df607e1babf913 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 119 ++-
 clang/test/Sema/attr-format-missing.c | 272 
 clang/test/Sema/attr-format-missing.cpp   | 303 ++
 8 files changed, 704 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3a84ff16a1e4d4..c6cd05da32332d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -381,6 +381,9 @@ Bug Fixes in This Version
 - Fixed a regression in CTAD that a friend declaration that befriends itself 
may cause
   incorrect constraint substitution. (#GH86769).
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH70024
+
 Bug Fixes to Compiler Builtins
 ^^
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 520168f01fd846..5c6a69ff1586db 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index df57f5e6ce11ba..d32bf4c8a9549e 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1008,6 +1008,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 8c98d8c7fef7a7..2bf2537027d728 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3885,6 +3885,10 @@ class Sema final {
   bool DiagnoseSwiftName(Decl *D, StringRef Name, SourceLocation Loc,
  const ParsedAttr , bool IsAsync);
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3dcd18b3afc8b4..77e01ba344e5a5 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8034,8 +8034,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 8bce04640e748e..fbf6f8f6f9534d 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -159,6 +159,13 @@ static bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+static bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 static inline bool isNSStringType(QualType T, ASTContext ,
   bool AllowNSAttributedString = false) {
   const auto *PT = T->getAs();
@@ -308,7 +315,7 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-04-10 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From e198dc5518f647fef0dff20f87dd01e2a187d892 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 119 ++-
 clang/test/Sema/attr-format-missing.c | 263 
 clang/test/Sema/attr-format-missing.cpp   | 293 ++
 8 files changed, 685 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3a84ff16a1e4d4..c6cd05da32332d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -381,6 +381,9 @@ Bug Fixes in This Version
 - Fixed a regression in CTAD that a friend declaration that befriends itself 
may cause
   incorrect constraint substitution. (#GH86769).
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH70024
+
 Bug Fixes to Compiler Builtins
 ^^
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 520168f01fd846..5c6a69ff1586db 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index df57f5e6ce11ba..d32bf4c8a9549e 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1008,6 +1008,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 8c98d8c7fef7a7..2bf2537027d728 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3885,6 +3885,10 @@ class Sema final {
   bool DiagnoseSwiftName(Decl *D, StringRef Name, SourceLocation Loc,
  const ParsedAttr , bool IsAsync);
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3dcd18b3afc8b4..77e01ba344e5a5 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8034,8 +8034,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 8bce04640e748e..fbf6f8f6f9534d 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -159,6 +159,13 @@ static bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+static bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 static inline bool isNSStringType(QualType T, ASTContext ,
   bool AllowNSAttributedString = false) {
   const auto *PT = T->getAs();
@@ -308,7 +315,7 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-04-09 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From c77435557c7b933f4e997b0fed64b37bb98fd8dd Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 119 +++-
 clang/test/Sema/attr-format-missing.c | 244 
 clang/test/Sema/attr-format-missing.cpp   | 261 ++
 8 files changed, 634 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3a84ff16a1e4d4..c6cd05da32332d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -381,6 +381,9 @@ Bug Fixes in This Version
 - Fixed a regression in CTAD that a friend declaration that befriends itself 
may cause
   incorrect constraint substitution. (#GH86769).
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH70024
+
 Bug Fixes to Compiler Builtins
 ^^
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 520168f01fd846..5c6a69ff1586db 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index df57f5e6ce11ba..d32bf4c8a9549e 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1008,6 +1008,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 8c98d8c7fef7a7..2bf2537027d728 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3885,6 +3885,10 @@ class Sema final {
   bool DiagnoseSwiftName(Decl *D, StringRef Name, SourceLocation Loc,
  const ParsedAttr , bool IsAsync);
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3dcd18b3afc8b4..77e01ba344e5a5 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8034,8 +8034,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 8bce04640e748e..fbf6f8f6f9534d 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -159,6 +159,13 @@ static bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+static bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 static inline bool isNSStringType(QualType T, ASTContext ,
   bool AllowNSAttributedString = false) {
   const auto *PT = T->getAs();
@@ -308,7 +315,7 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-04-09 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 24d342d4d3a432a6f8e1e01e410a6e9dbeb5e0dd Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 119 +++-
 clang/test/Sema/attr-format-missing.c | 244 
 clang/test/Sema/attr-format-missing.cpp   | 261 ++
 8 files changed, 634 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3a84ff16a1e4d4..c6cd05da32332d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -381,6 +381,9 @@ Bug Fixes in This Version
 - Fixed a regression in CTAD that a friend declaration that befriends itself 
may cause
   incorrect constraint substitution. (#GH86769).
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH70024
+
 Bug Fixes to Compiler Builtins
 ^^
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 520168f01fd846..5c6a69ff1586db 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index df57f5e6ce11ba..d32bf4c8a9549e 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1008,6 +1008,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 8c98d8c7fef7a7..2bf2537027d728 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3885,6 +3885,10 @@ class Sema final {
   bool DiagnoseSwiftName(Decl *D, StringRef Name, SourceLocation Loc,
  const ParsedAttr , bool IsAsync);
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo ,
   StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3dcd18b3afc8b4..77e01ba344e5a5 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8034,8 +8034,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 8bce04640e748e..4ea2167b1ed472 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -159,6 +159,13 @@ static bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+static bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 static inline bool isNSStringType(QualType T, ASTContext ,
   bool AllowNSAttributedString = false) {
   const auto *PT = T->getAs();
@@ -308,7 +315,7 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-04-05 Thread Vlad Serebrennikov via cfe-commits


@@ -1698,6 +1698,9 @@ class Sema final {
 ChangedStateAtExit
   };
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,

Endilll wrote:

This declaration is placed among functions implemented in `SemaAttr.cpp`, as 
you might see around line 1296, but its implementation is placed in 
`SemaDeclAttr.cpp`. Declarations of functions in this file have their own 
section in `Sema.h`, which starts around line 3690. This discrepancy should be 
fixed one way or another.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-04-05 Thread Vlad Serebrennikov via cfe-commits

https://github.com/Endilll requested changes to this pull request.


https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-04-05 Thread Vlad Serebrennikov via cfe-commits

https://github.com/Endilll edited 
https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-04-05 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia reopened 
https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-04-05 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 184fa1b21011d3700f2d501c49882bb87fcb5f38 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   3 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 117 -
 clang/test/Sema/attr-format-missing.c | 223 
 clang/test/Sema/attr-format-missing.cpp   | 238 ++
 8 files changed, 587 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3a84ff16a1e4d4..c6cd05da32332d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -381,6 +381,9 @@ Bug Fixes in This Version
 - Fixed a regression in CTAD that a friend declaration that befriends itself 
may cause
   incorrect constraint substitution. (#GH86769).
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH70024
+
 Bug Fixes to Compiler Builtins
 ^^
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 520168f01fd846..5c6a69ff1586db 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -505,7 +505,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index df57f5e6ce11ba..d32bf4c8a9549e 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1008,6 +1008,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 8c98d8c7fef7a7..1d57cb8779d7d0 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -1698,6 +1698,9 @@ class Sema final {
 ChangedStateAtExit
   };
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
   void DiagnoseNonDefaultPragmaAlignPack(PragmaAlignPackDiagnoseKind Kind,
  SourceLocation IncludeLoc);
   void DiagnoseUnterminatedPragmaAlignPack();
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3dcd18b3afc8b4..77e01ba344e5a5 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8034,8 +8034,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 8bce04640e748e..281cae2bfa52de 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -159,6 +159,13 @@ static bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+static bool checkIfMethodHasImplicitThisParam(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast(D))
+return MethodDecl->isInstance() &&
+   !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 static inline bool isNSStringType(QualType T, ASTContext ,
   bool AllowNSAttributedString = false) {
   const auto *PT = T->getAs();
@@ -308,7 +315,7 @@ static bool checkFunctionOrMethodParameterIndex(
   // 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-04-02 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 1c027637176d9100d3cc0a4f92b4ee227d4afddf Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 13 Oct 2023 14:45:15 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   4 +-
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 105 +
 clang/test/Sema/attr-format-missing.c | 223 ++
 clang/test/Sema/attr-format-missing.cpp   | 211 +
 8 files changed, 552 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9cc2a72f4c864d..1ca055f65f2115 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -65,7 +65,9 @@ Improvements to Clang's diagnostics
 ^^^
 - We now generate a diagnostic for signed integer overflow due to unary minus
   in a non-constant expression context. This fixes
-  `Issue 31643 `_
+  `Issue 31643 `_.
+- We now generate a diagnostic for missing format attributes
+  `Issue 60718 `_.
 
 Non-comprehensive list of changes in this release
 -
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 17fdcffa2d4274..114cbbc2e82b85 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -482,7 +482,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 6d6f474f6dcdab..964166b70c2aee 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -936,6 +936,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 741c2503127af7..064506e7096033 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -10615,6 +10615,10 @@ class Sema final {
 ChangedStateAtExit
   };
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   void DiagnoseNonDefaultPragmaAlignPack(PragmaAlignPackDiagnoseKind Kind,
  SourceLocation IncludeLoc);
   void DiagnoseUnterminatedPragmaAlignPack();
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 4602284309491c..d3ac6cb519c56e 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -6014,8 +6014,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 1a0bfb3d91bcc8..b30d752a36cd49 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -6849,6 +6849,111 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, 
const ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// This function is called only if function call is not inside template body.
+// TODO: Add call for function calls inside template body.
+// Check if parent function misses format 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-04-02 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 7adedf54a6c4d509046915777600b6e66b90bb8c Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 13 Oct 2023 14:45:15 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   4 +-
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 105 +
 clang/test/Sema/attr-format-missing.c | 223 ++
 clang/test/Sema/attr-format-missing.cpp   | 211 +
 8 files changed, 552 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9cc2a72f4c864d..1ca055f65f2115 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -65,7 +65,9 @@ Improvements to Clang's diagnostics
 ^^^
 - We now generate a diagnostic for signed integer overflow due to unary minus
   in a non-constant expression context. This fixes
-  `Issue 31643 `_
+  `Issue 31643 `_.
+- We now generate a diagnostic for missing format attributes
+  `Issue 60718 `_.
 
 Non-comprehensive list of changes in this release
 -
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 17fdcffa2d4274..114cbbc2e82b85 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -482,7 +482,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 6d6f474f6dcdab..964166b70c2aee 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -936,6 +936,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 741c2503127af7..064506e7096033 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -10615,6 +10615,10 @@ class Sema final {
 ChangedStateAtExit
   };
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   void DiagnoseNonDefaultPragmaAlignPack(PragmaAlignPackDiagnoseKind Kind,
  SourceLocation IncludeLoc);
   void DiagnoseUnterminatedPragmaAlignPack();
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 4602284309491c..d3ac6cb519c56e 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -6014,8 +6014,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 1a0bfb3d91bcc8..f2134f2aac70bd 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -6849,6 +6849,111 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, 
const ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// This function is called only if function call is not inside template body.
+// TODO: Add call for function calls inside template body.
+// Check if parent function misses format 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-29 Thread Aaron Ballman via cfe-commits


@@ -7096,6 +7096,111 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, 
const ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// This function is called only if function call is not inside template body.
+// TODO: Add call for function calls inside template body.
+// Check if parent function misses format attribute. If misses, emit warning.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc) {
+  assert(FDecl);
+
+  const FunctionDecl *ParentFuncDecl = getCurFunctionDecl();
+  if (!ParentFuncDecl)
+return;
+
+  // If function is a member of struct/union/class, format attribute argument
+  // indexing starts from 2. Otherwise, it starts from 1.
+  const unsigned int FormatArgumentIndexOffset =
+  isInstanceMethod(FDecl) ? 2 : 1;
+  const unsigned int ParentFunctionFormatArgumentIndexOffset =
+  isInstanceMethod(ParentFuncDecl) ? 2 : 1;

AaronBallman wrote:

```suggestion
  unsigned int FormatArgumentIndexOffset =
  isInstanceMethod(FDecl) ? 2 : 1;
  unsigned int ParentFunctionFormatArgumentIndexOffset =
  isInstanceMethod(ParentFuncDecl) ? 2 : 1;
```
We don't generally use top-level `const` in the project.

Be sure to add test coverage for explicit object instance methods (C++23) to 
make sure the indexing behavior still makes sense.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-29 Thread Aaron Ballman via cfe-commits


@@ -0,0 +1,211 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+
+#include 
+#include 

AaronBallman wrote:

Same issue here with includes as in the C file.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-29 Thread Aaron Ballman via cfe-commits


@@ -288,6 +288,9 @@ Improvements to Clang's diagnostics
 
 - Clang now correctly diagnoses no arguments to a variadic macro parameter as 
a C23/C++20 extension.
   Fixes #GH84495.
+ 
+- Clang now diagnoses missing format attributes for non-template functions and 
class/struct/union members
+  Fixes #GH70024

AaronBallman wrote:

```suggestion
- Clang now diagnoses missing format attributes for non-template functions and
  class/struct/union members. Fixes #GH70024
```
Formatting and punctuation, just nits.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-29 Thread Aaron Ballman via cfe-commits


@@ -7096,6 +7096,111 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, 
const ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// This function is called only if function call is not inside template body.
+// TODO: Add call for function calls inside template body.
+// Check if parent function misses format attribute. If misses, emit warning.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc) {
+  assert(FDecl);
+
+  const FunctionDecl *ParentFuncDecl = getCurFunctionDecl();
+  if (!ParentFuncDecl)
+return;
+
+  // If function is a member of struct/union/class, format attribute argument
+  // indexing starts from 2. Otherwise, it starts from 1.
+  const unsigned int FormatArgumentIndexOffset =
+  isInstanceMethod(FDecl) ? 2 : 1;
+  const unsigned int ParentFunctionFormatArgumentIndexOffset =
+  isInstanceMethod(ParentFuncDecl) ? 2 : 1;
+
+  // Check if function has format attribute with forwarded format string.
+  IdentifierInfo *AttrType;
+  const ParmVarDecl *FormatArg;
+  if (!llvm::any_of(
+  FDecl->specific_attrs(), [&](const FormatAttr *Attr) {
+const int FormatIndexOffseted =
+Attr->getFormatIdx() - FormatArgumentIndexOffset;
+if (FormatIndexOffseted < 0 ||
+(unsigned)FormatIndexOffseted >= Args.size())
+  return false;
+
+const DeclRefExpr *FormatArgExpr = dyn_cast_or_null(
+Args[FormatIndexOffseted]->IgnoreParenCasts());
+if (!FormatArgExpr)
+  return false;
+
+FormatArg = dyn_cast_or_null(
+FormatArgExpr->getReferencedDeclOfCallee());
+if (!FormatArg)
+  return false;
+
+AttrType = Attr->getType();
+return true;
+  }))
+return;
+
+  // Check if format string argument is parent function parameter.
+  unsigned int StringIndex = 0;
+  if (!llvm::any_of(ParentFuncDecl->parameters(),
+[&](const ParmVarDecl *Param) {
+  StringIndex = Param->getFunctionScopeIndex() +
+ParentFunctionFormatArgumentIndexOffset;
+
+  return Param == FormatArg;
+}))
+return;
+
+  unsigned NumOfParentFunctionParams = ParentFuncDecl->getNumParams();
+
+  // Compare parent and calling function format attribute arguments (archetype
+  // and format string).
+  if (llvm::any_of(
+  ParentFuncDecl->specific_attrs(),
+  [&](const FormatAttr *Attr) {
+if (Attr->getType() != AttrType)
+  return false;
+const int FormatIndexOffseted =
+Attr->getFormatIdx() - ParentFunctionFormatArgumentIndexOffset;
+
+if (FormatIndexOffseted < 0 ||
+(unsigned)FormatIndexOffseted >= NumOfParentFunctionParams)
+  return false;
+
+if (ParentFuncDecl->parameters()[FormatIndexOffseted] != FormatArg)
+  return false;
+
+return true;
+  }))
+return;
+
+  // If parent function is variadic, check if last argument of child function 
is
+  // va_list.
+  unsigned FirstToCheck = [&]() -> unsigned {
+if (!ParentFuncDecl->isVariadic())
+  return 0;
+const DeclRefExpr *FirstToCheckArg = dyn_cast_or_null(

AaronBallman wrote:

```suggestion
const auto *FirstToCheckArg = dyn_cast(
```

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-29 Thread Aaron Ballman via cfe-commits

https://github.com/AaronBallman edited 
https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-29 Thread Aaron Ballman via cfe-commits


@@ -7096,6 +7096,111 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, 
const ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// This function is called only if function call is not inside template body.
+// TODO: Add call for function calls inside template body.
+// Check if parent function misses format attribute. If misses, emit warning.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc) {
+  assert(FDecl);
+
+  const FunctionDecl *ParentFuncDecl = getCurFunctionDecl();
+  if (!ParentFuncDecl)
+return;
+
+  // If function is a member of struct/union/class, format attribute argument
+  // indexing starts from 2. Otherwise, it starts from 1.
+  const unsigned int FormatArgumentIndexOffset =
+  isInstanceMethod(FDecl) ? 2 : 1;
+  const unsigned int ParentFunctionFormatArgumentIndexOffset =
+  isInstanceMethod(ParentFuncDecl) ? 2 : 1;
+
+  // Check if function has format attribute with forwarded format string.
+  IdentifierInfo *AttrType;
+  const ParmVarDecl *FormatArg;
+  if (!llvm::any_of(
+  FDecl->specific_attrs(), [&](const FormatAttr *Attr) {
+const int FormatIndexOffseted =
+Attr->getFormatIdx() - FormatArgumentIndexOffset;
+if (FormatIndexOffseted < 0 ||
+(unsigned)FormatIndexOffseted >= Args.size())
+  return false;
+
+const DeclRefExpr *FormatArgExpr = dyn_cast_or_null(
+Args[FormatIndexOffseted]->IgnoreParenCasts());
+if (!FormatArgExpr)
+  return false;
+
+FormatArg = dyn_cast_or_null(
+FormatArgExpr->getReferencedDeclOfCallee());
+if (!FormatArg)
+  return false;
+
+AttrType = Attr->getType();
+return true;
+  }))
+return;
+
+  // Check if format string argument is parent function parameter.
+  unsigned int StringIndex = 0;
+  if (!llvm::any_of(ParentFuncDecl->parameters(),
+[&](const ParmVarDecl *Param) {
+  StringIndex = Param->getFunctionScopeIndex() +
+ParentFunctionFormatArgumentIndexOffset;
+
+  return Param == FormatArg;
+}))
+return;
+
+  unsigned NumOfParentFunctionParams = ParentFuncDecl->getNumParams();
+
+  // Compare parent and calling function format attribute arguments (archetype
+  // and format string).
+  if (llvm::any_of(
+  ParentFuncDecl->specific_attrs(),
+  [&](const FormatAttr *Attr) {
+if (Attr->getType() != AttrType)
+  return false;
+const int FormatIndexOffseted =
+Attr->getFormatIdx() - ParentFunctionFormatArgumentIndexOffset;
+
+if (FormatIndexOffseted < 0 ||
+(unsigned)FormatIndexOffseted >= NumOfParentFunctionParams)
+  return false;
+
+if (ParentFuncDecl->parameters()[FormatIndexOffseted] != FormatArg)
+  return false;
+
+return true;
+  }))
+return;
+
+  // If parent function is variadic, check if last argument of child function 
is
+  // va_list.
+  unsigned FirstToCheck = [&]() -> unsigned {
+if (!ParentFuncDecl->isVariadic())
+  return 0;
+const DeclRefExpr *FirstToCheckArg = dyn_cast_or_null(
+Args[Args.size() - 1]->IgnoreParenCasts());
+if (!FirstToCheckArg)
+  return 0;
+
+if (FirstToCheckArg->getType().getAsString() != "va_list")
+  return 0;
+return NumOfParentFunctionParams + ParentFunctionFormatArgumentIndexOffset;
+  }();
+
+  // Emit warning
+  std::string InsertionText;
+  llvm::raw_string_ostream OS(InsertionText);
+  OS << "__attribute__((format(" << AttrType->getName() << ", " << StringIndex
+ << ", " << FirstToCheck << ")))";

AaronBallman wrote:

I think you can use `llvm::Twine` instead of a string stream.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-29 Thread Aaron Ballman via cfe-commits


@@ -0,0 +1,223 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+
+#include 
+#include 
+#include 
+#include 

AaronBallman wrote:

Our test files should be hermetic and not rely on anything from the test system 
itself. `stdarg.h` is fine because we provide that as part of Clang, but the 
rest are all being pulled from the system. You should use forward declarations 
or other tricks so that these can be removed.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-29 Thread Aaron Ballman via cfe-commits


@@ -7096,6 +7096,111 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, 
const ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// This function is called only if function call is not inside template body.
+// TODO: Add call for function calls inside template body.
+// Check if parent function misses format attribute. If misses, emit warning.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc) {
+  assert(FDecl);
+
+  const FunctionDecl *ParentFuncDecl = getCurFunctionDecl();
+  if (!ParentFuncDecl)
+return;
+
+  // If function is a member of struct/union/class, format attribute argument
+  // indexing starts from 2. Otherwise, it starts from 1.
+  const unsigned int FormatArgumentIndexOffset =
+  isInstanceMethod(FDecl) ? 2 : 1;
+  const unsigned int ParentFunctionFormatArgumentIndexOffset =
+  isInstanceMethod(ParentFuncDecl) ? 2 : 1;
+
+  // Check if function has format attribute with forwarded format string.
+  IdentifierInfo *AttrType;
+  const ParmVarDecl *FormatArg;
+  if (!llvm::any_of(
+  FDecl->specific_attrs(), [&](const FormatAttr *Attr) {
+const int FormatIndexOffseted =
+Attr->getFormatIdx() - FormatArgumentIndexOffset;
+if (FormatIndexOffseted < 0 ||
+(unsigned)FormatIndexOffseted >= Args.size())
+  return false;
+
+const DeclRefExpr *FormatArgExpr = dyn_cast_or_null(
+Args[FormatIndexOffseted]->IgnoreParenCasts());
+if (!FormatArgExpr)
+  return false;
+
+FormatArg = dyn_cast_or_null(
+FormatArgExpr->getReferencedDeclOfCallee());
+if (!FormatArg)
+  return false;
+
+AttrType = Attr->getType();
+return true;
+  }))
+return;
+
+  // Check if format string argument is parent function parameter.
+  unsigned int StringIndex = 0;
+  if (!llvm::any_of(ParentFuncDecl->parameters(),
+[&](const ParmVarDecl *Param) {
+  StringIndex = Param->getFunctionScopeIndex() +
+ParentFunctionFormatArgumentIndexOffset;
+
+  return Param == FormatArg;
+}))
+return;
+
+  unsigned NumOfParentFunctionParams = ParentFuncDecl->getNumParams();
+
+  // Compare parent and calling function format attribute arguments (archetype
+  // and format string).
+  if (llvm::any_of(
+  ParentFuncDecl->specific_attrs(),
+  [&](const FormatAttr *Attr) {
+if (Attr->getType() != AttrType)
+  return false;
+const int FormatIndexOffseted =

AaronBallman wrote:

```suggestion
int FormatIndexOffseted =
```

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-29 Thread Aaron Ballman via cfe-commits


@@ -7096,6 +7096,111 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, 
const ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// This function is called only if function call is not inside template body.
+// TODO: Add call for function calls inside template body.
+// Check if parent function misses format attribute. If misses, emit warning.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc) {
+  assert(FDecl);
+
+  const FunctionDecl *ParentFuncDecl = getCurFunctionDecl();
+  if (!ParentFuncDecl)
+return;
+
+  // If function is a member of struct/union/class, format attribute argument
+  // indexing starts from 2. Otherwise, it starts from 1.
+  const unsigned int FormatArgumentIndexOffset =
+  isInstanceMethod(FDecl) ? 2 : 1;
+  const unsigned int ParentFunctionFormatArgumentIndexOffset =
+  isInstanceMethod(ParentFuncDecl) ? 2 : 1;
+
+  // Check if function has format attribute with forwarded format string.
+  IdentifierInfo *AttrType;
+  const ParmVarDecl *FormatArg;
+  if (!llvm::any_of(
+  FDecl->specific_attrs(), [&](const FormatAttr *Attr) {
+const int FormatIndexOffseted =
+Attr->getFormatIdx() - FormatArgumentIndexOffset;
+if (FormatIndexOffseted < 0 ||
+(unsigned)FormatIndexOffseted >= Args.size())
+  return false;
+
+const DeclRefExpr *FormatArgExpr = dyn_cast_or_null(
+Args[FormatIndexOffseted]->IgnoreParenCasts());

AaronBallman wrote:

```suggestion
const auto *FormatArgExpr = dyn_cast(
Args[FormatIndexOffseted]->IgnoreParenCasts());
```
Can drop the `or_null` because we're relying on `Args[FormatIndexOffseted]` to 
be non-null already.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-29 Thread Aaron Ballman via cfe-commits


@@ -7096,6 +7096,111 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, 
const ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// This function is called only if function call is not inside template body.
+// TODO: Add call for function calls inside template body.
+// Check if parent function misses format attribute. If misses, emit warning.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc) {
+  assert(FDecl);
+
+  const FunctionDecl *ParentFuncDecl = getCurFunctionDecl();
+  if (!ParentFuncDecl)
+return;
+
+  // If function is a member of struct/union/class, format attribute argument
+  // indexing starts from 2. Otherwise, it starts from 1.
+  const unsigned int FormatArgumentIndexOffset =
+  isInstanceMethod(FDecl) ? 2 : 1;
+  const unsigned int ParentFunctionFormatArgumentIndexOffset =
+  isInstanceMethod(ParentFuncDecl) ? 2 : 1;
+
+  // Check if function has format attribute with forwarded format string.
+  IdentifierInfo *AttrType;
+  const ParmVarDecl *FormatArg;
+  if (!llvm::any_of(
+  FDecl->specific_attrs(), [&](const FormatAttr *Attr) {
+const int FormatIndexOffseted =
+Attr->getFormatIdx() - FormatArgumentIndexOffset;
+if (FormatIndexOffseted < 0 ||
+(unsigned)FormatIndexOffseted >= Args.size())
+  return false;
+
+const DeclRefExpr *FormatArgExpr = dyn_cast_or_null(
+Args[FormatIndexOffseted]->IgnoreParenCasts());
+if (!FormatArgExpr)
+  return false;
+
+FormatArg = dyn_cast_or_null(
+FormatArgExpr->getReferencedDeclOfCallee());
+if (!FormatArg)
+  return false;
+
+AttrType = Attr->getType();
+return true;
+  }))
+return;
+
+  // Check if format string argument is parent function parameter.
+  unsigned int StringIndex = 0;
+  if (!llvm::any_of(ParentFuncDecl->parameters(),
+[&](const ParmVarDecl *Param) {
+  StringIndex = Param->getFunctionScopeIndex() +
+ParentFunctionFormatArgumentIndexOffset;
+
+  return Param == FormatArg;
+}))
+return;
+
+  unsigned NumOfParentFunctionParams = ParentFuncDecl->getNumParams();
+
+  // Compare parent and calling function format attribute arguments (archetype
+  // and format string).
+  if (llvm::any_of(
+  ParentFuncDecl->specific_attrs(),
+  [&](const FormatAttr *Attr) {
+if (Attr->getType() != AttrType)
+  return false;
+const int FormatIndexOffseted =
+Attr->getFormatIdx() - ParentFunctionFormatArgumentIndexOffset;
+
+if (FormatIndexOffseted < 0 ||
+(unsigned)FormatIndexOffseted >= NumOfParentFunctionParams)
+  return false;
+
+if (ParentFuncDecl->parameters()[FormatIndexOffseted] != FormatArg)
+  return false;
+
+return true;
+  }))
+return;
+
+  // If parent function is variadic, check if last argument of child function 
is
+  // va_list.
+  unsigned FirstToCheck = [&]() -> unsigned {
+if (!ParentFuncDecl->isVariadic())
+  return 0;
+const DeclRefExpr *FirstToCheckArg = dyn_cast_or_null(
+Args[Args.size() - 1]->IgnoreParenCasts());
+if (!FirstToCheckArg)
+  return 0;
+
+if (FirstToCheckArg->getType().getAsString() != "va_list")
+  return 0;

AaronBallman wrote:

Rather than doing a string comparison here, can we check if the canonical type 
equals `ASTContext::getBuiltinVaListType()`? Then that handles situations like 
the user using a typedef to `va_list`, etc (which would be a good test case to 
add).

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-29 Thread Aaron Ballman via cfe-commits

https://github.com/AaronBallman commented:

Precommit CI found related failures that should be addressed.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-29 Thread Budimir Aranđelović via cfe-commits

budimirarandjelovicsyrmia wrote:

ping

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-25 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 7adedf54a6c4d509046915777600b6e66b90bb8c Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 13 Oct 2023 14:45:15 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   4 +-
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 105 +
 clang/test/Sema/attr-format-missing.c | 223 ++
 clang/test/Sema/attr-format-missing.cpp   | 211 +
 8 files changed, 552 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9cc2a72f4c864d..1ca055f65f2115 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -65,7 +65,9 @@ Improvements to Clang's diagnostics
 ^^^
 - We now generate a diagnostic for signed integer overflow due to unary minus
   in a non-constant expression context. This fixes
-  `Issue 31643 `_
+  `Issue 31643 `_.
+- We now generate a diagnostic for missing format attributes
+  `Issue 60718 `_.
 
 Non-comprehensive list of changes in this release
 -
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 17fdcffa2d4274..114cbbc2e82b85 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -482,7 +482,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 6d6f474f6dcdab..964166b70c2aee 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -936,6 +936,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 741c2503127af7..064506e7096033 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -10615,6 +10615,10 @@ class Sema final {
 ChangedStateAtExit
   };
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   void DiagnoseNonDefaultPragmaAlignPack(PragmaAlignPackDiagnoseKind Kind,
  SourceLocation IncludeLoc);
   void DiagnoseUnterminatedPragmaAlignPack();
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 4602284309491c..d3ac6cb519c56e 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -6014,8 +6014,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 1a0bfb3d91bcc8..f2134f2aac70bd 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -6849,6 +6849,111 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, 
const ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// This function is called only if function call is not inside template body.
+// TODO: Add call for function calls inside template body.
+// Check if parent function misses format 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-25 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 7adedf54a6c4d509046915777600b6e66b90bb8c Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 13 Oct 2023 14:45:15 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   4 +-
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 105 +
 clang/test/Sema/attr-format-missing.c | 223 ++
 clang/test/Sema/attr-format-missing.cpp   | 211 +
 8 files changed, 552 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9cc2a72f4c864d..1ca055f65f2115 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -65,7 +65,9 @@ Improvements to Clang's diagnostics
 ^^^
 - We now generate a diagnostic for signed integer overflow due to unary minus
   in a non-constant expression context. This fixes
-  `Issue 31643 `_
+  `Issue 31643 `_.
+- We now generate a diagnostic for missing format attributes
+  `Issue 60718 `_.
 
 Non-comprehensive list of changes in this release
 -
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 17fdcffa2d4274..114cbbc2e82b85 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -482,7 +482,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 6d6f474f6dcdab..964166b70c2aee 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -936,6 +936,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 741c2503127af7..064506e7096033 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -10615,6 +10615,10 @@ class Sema final {
 ChangedStateAtExit
   };
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   void DiagnoseNonDefaultPragmaAlignPack(PragmaAlignPackDiagnoseKind Kind,
  SourceLocation IncludeLoc);
   void DiagnoseUnterminatedPragmaAlignPack();
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 4602284309491c..d3ac6cb519c56e 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -6014,8 +6014,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 1a0bfb3d91bcc8..f2134f2aac70bd 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -6849,6 +6849,111 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, 
const ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// This function is called only if function call is not inside template body.
+// TODO: Add call for function calls inside template body.
+// Check if parent function misses format 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-23 Thread Vlad Serebrennikov via cfe-commits

Endilll wrote:

I'm not able to review Sema.h changes because of a merge conflict.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-20 Thread Budimir Aranđelović via cfe-commits


@@ -6849,6 +6849,71 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, const 
ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// Warn if parent function misses format attribute. Parent function misses
+// format attribute if there is an argument format string forwarded to calling
+// function with format attribute, parent function has a parameter which is
+// either char or string or pointer to char and parent function format 
attribute
+// type does not match with calling function format attribute type.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc) {
+  assert(FDecl);
+
+  // Check if function has format attribute with forwarded format string.
+  IdentifierInfo *AttrType;
+  if (!llvm::any_of(
+  FDecl->specific_attrs(), [&](const FormatAttr *Attr) {
+if (!Args[Attr->getFirstArg()]->getReferencedDeclOfCallee())
+  return false;
+
+AttrType = Attr->getType();
+return true;
+  }))
+return;
+
+  const FunctionDecl *ParentFuncDecl = getCurFunctionDecl();
+  if (!ParentFuncDecl)
+return;
+
+  // Check if parent function has char, string or pointer to char parameter.
+  unsigned int StringIndex = 0;
+  if (!llvm::any_of(
+  ParentFuncDecl->parameters(), [&](const ParmVarDecl *Param) {
+StringIndex = Param->getFunctionScopeIndex() + 1;
+QualType Ty = Param->getType();
+if (isNSStringType(Ty, Context, true))
+  return true;
+if (isCFStringType(Ty, Context))
+  return true;
+if (Ty->isPointerType() &&
+Ty->castAs()->getPointeeType()->isCharType())

budimirarandjelovicsyrmia wrote:

Let this case remains for future addressing.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-20 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From 7adedf54a6c4d509046915777600b6e66b90bb8c Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 13 Oct 2023 14:45:15 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   4 +-
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 105 +
 clang/test/Sema/attr-format-missing.c | 223 ++
 clang/test/Sema/attr-format-missing.cpp   | 211 +
 8 files changed, 552 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9cc2a72f4c864d..1ca055f65f2115 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -65,7 +65,9 @@ Improvements to Clang's diagnostics
 ^^^
 - We now generate a diagnostic for signed integer overflow due to unary minus
   in a non-constant expression context. This fixes
-  `Issue 31643 `_
+  `Issue 31643 `_.
+- We now generate a diagnostic for missing format attributes
+  `Issue 60718 `_.
 
 Non-comprehensive list of changes in this release
 -
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 17fdcffa2d4274..114cbbc2e82b85 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -482,7 +482,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 6d6f474f6dcdab..964166b70c2aee 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -936,6 +936,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 741c2503127af7..064506e7096033 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -10615,6 +10615,10 @@ class Sema final {
 ChangedStateAtExit
   };
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   void DiagnoseNonDefaultPragmaAlignPack(PragmaAlignPackDiagnoseKind Kind,
  SourceLocation IncludeLoc);
   void DiagnoseUnterminatedPragmaAlignPack();
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 4602284309491c..d3ac6cb519c56e 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -6014,8 +6014,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 1a0bfb3d91bcc8..f2134f2aac70bd 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -6849,6 +6849,111 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, 
const ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// This function is called only if function call is not inside template body.
+// TODO: Add call for function calls inside template body.
+// Check if parent function misses format 

[clang] [clang] Catch missing format attributes (PR #70024)

2024-03-18 Thread Budimir Aranđelović via cfe-commits


@@ -6849,6 +6849,71 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, const 
ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// Warn if parent function misses format attribute. Parent function misses
+// format attribute if there is an argument format string forwarded to calling
+// function with format attribute, parent function has a parameter which is
+// either char or string or pointer to char and parent function format 
attribute
+// type does not match with calling function format attribute type.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc) {
+  assert(FDecl);
+
+  // Check if function has format attribute with forwarded format string.
+  IdentifierInfo *AttrType;
+  if (!llvm::any_of(
+  FDecl->specific_attrs(), [&](const FormatAttr *Attr) {
+if (!Args[Attr->getFirstArg()]->getReferencedDeclOfCallee())
+  return false;
+
+AttrType = Attr->getType();
+return true;
+  }))
+return;
+
+  const FunctionDecl *ParentFuncDecl = getCurFunctionDecl();
+  if (!ParentFuncDecl)
+return;
+
+  // Check if parent function has char, string or pointer to char parameter.
+  unsigned int StringIndex = 0;
+  if (!llvm::any_of(
+  ParentFuncDecl->parameters(), [&](const ParmVarDecl *Param) {
+StringIndex = Param->getFunctionScopeIndex() + 1;
+QualType Ty = Param->getType();
+if (isNSStringType(Ty, Context, true))
+  return true;
+if (isCFStringType(Ty, Context))
+  return true;
+if (Ty->isPointerType() &&
+Ty->castAs()->getPointeeType()->isCharType())

budimirarandjelovicsyrmia wrote:

By analyzing LLVM code, I found that calls inside template body are built 
differently than calls inside non-template body. It is obvious for 
template-dependent calls, as these calls are not built as resolved expression 
calls. Regarding template-independent calls, they can be processed as resolved 
expression calls, but function checkCall(), where is diagnosing, does not make 
checks for these calls. At the beggining of checkCall() there are FIX-ME 
comment which says that template calls should be checked in template definition.
It would be significant for me if you know if there is a part of code that 
process content of template body and definition. Otherwise, I think it is okay 
to postpone addressing this comment.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-01-09 Thread Budimir Aranđelović via cfe-commits


@@ -0,0 +1,132 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+
+#include 
+#include 
+
+void f1(const std::string , ... /* args */)
+{
+va_list args;
+vscanf(str.c_str(), args); // no warning
+vprintf(str.c_str(), args); // no warning
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error: {{format argument not 
a string type}}
+void f2(const std::string , ... /* args */);
+
+void f3(std::string_view str, ... /* args */)
+{
+va_list args;
+vscanf(std::string(str).c_str(), args); // no warning
+vprintf(std::string(str).c_str(), args); // no warning
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f4(std::string_view str, ... /* args */);
+
+void f5(const std::wstring , ... /* args */)
+{
+va_list args;
+vscanf((const char *)str.c_str(), args); // no warning
+vprintf((const char *)str.c_str(), args); // no warning
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f6(const std::wstring , ... /* args */);
+
+void f7(std::wstring_view str, ... /* args */)
+{
+va_list args;
+vscanf((const char *) std::wstring(str).c_str(), args); // no warning
+vprintf((const char *) std::wstring(str).c_str(), args); // no warning
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f8(std::wstring_view str, ... /* args */);
+
+void f9(const wchar_t *out, ... /* args */)
+{
+va_list args;
+vprintf(out, args); // expected-error {{no matching function for call to 
'vprintf'}}
+vscanf((const char *) out, args); // no warning
+vscanf((char *) out, args); // no warning
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f10(const wchar_t *out, ... /* args */);
+
+void f11(const char16_t *out, ... /* args */)
+{
+va_list args;
+vscanf(out, args); // expected-error {{no matching function for call to 
'vscanf'}}
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f12(const char16_t *out, ... /* args */);
+
+void f13(const char32_t *out, ... /* args */)
+{
+va_list args;
+vscanf(out, args); // expected-error {{no matching function for call to 
'vscanf'}}
+}
+
+__attribute__((format(scanf, 1, 2))) // expected-error {{format argument not a 
string type}}
+void f14(const char32_t *out, ... /* args */);
+
+void f15(const char *out, ... /* args */)
+{
+va_list args;
+vscanf(out, args); // expected-warning {{diagnostic behavior may be 
improved by adding the 'scanf' format attribute to the declaration of 'f15'}}
+}
+
+__attribute__((format(scanf, 1, 2)))
+void f16(const char *out, ... /* args */)
+{
+va_list args;
+vscanf(out, args); // no warning
+}
+
+void f17(const unsigned char *out, ... /* args */)
+{
+va_list args;
+vscanf(out, args); // expected-error {{no matching function for call to 
'vscanf'}}
+}
+
+__attribute__((format(scanf, 1, 2)))
+void f18(const unsigned char *out, ... /* args */)
+{
+va_list args;
+vscanf(out, args); // expected-error {{no matching function for call to 
'vscanf'}}
+}
+
+void f19(const signed char *out, ... /* args */)
+{
+va_list args;
+vscanf(out, args); // expected-error {{no matching function for call to 
'vscanf'}}
+}
+
+__attribute__((format(scanf, 1, 2)))
+void f20(const signed char *out, ... /* args */)
+{
+va_list args;
+vscanf(out, args); // expected-error {{no matching function for call to 
'vscanf'}}
+}
+
+void f21(const char out[], ... /* args */)
+{
+va_list args;
+vscanf(out, args); // expected-warning {{diagnostic behavior may be 
improved by adding the 'scanf' format attribute to the declaration of 'f21'}}
+}
+
+__attribute__((format(scanf, 1, 2)))
+void f22(const char out[], ... /* args */)
+{
+va_list args;
+vscanf(out, args); // no warning
+}
+
+template 
+void func(char ()[N], ...)
+{
+va_list args;
+vprintf(str, args); // no warning
+}

budimirarandjelovicsyrmia wrote:

Added tests where function is member of a class/structure/union.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2024-01-09 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From ca3bfcd4052276b0a776f6acfefcd83bbeab28a1 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 13 Oct 2023 14:45:15 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   4 +-
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   | 103 
 clang/test/Sema/attr-format-missing.c | 223 ++
 clang/test/Sema/attr-format-missing.cpp   | 211 +
 8 files changed, 550 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9cc2a72f4c864d..1ca055f65f2115 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -65,7 +65,9 @@ Improvements to Clang's diagnostics
 ^^^
 - We now generate a diagnostic for signed integer overflow due to unary minus
   in a non-constant expression context. This fixes
-  `Issue 31643 `_
+  `Issue 31643 `_.
+- We now generate a diagnostic for missing format attributes
+  `Issue 60718 `_.
 
 Non-comprehensive list of changes in this release
 -
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 17fdcffa2d4274..114cbbc2e82b85 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -482,7 +482,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 6d6f474f6dcdab..964166b70c2aee 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -936,6 +936,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 741c2503127af7..064506e7096033 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -10615,6 +10615,10 @@ class Sema final {
 ChangedStateAtExit
   };
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   void DiagnoseNonDefaultPragmaAlignPack(PragmaAlignPackDiagnoseKind Kind,
  SourceLocation IncludeLoc);
   void DiagnoseUnterminatedPragmaAlignPack();
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 4602284309491c..d3ac6cb519c56e 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -6014,8 +6014,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 1a0bfb3d91bcc8..305ee1c230ffb3 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -6849,6 +6849,109 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, 
const ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// Check if parent function misses format attribute. If misses, emit warning.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+ 

[clang] [clang] Catch missing format attributes (PR #70024)

2023-12-21 Thread Budimir Aranđelović via cfe-commits


@@ -6849,6 +6849,73 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, const 
ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// Warn if parent function misses format attribute. Parent function misses
+// format attribute if there is an argument format string forwarded to calling
+// function with format attribute, parent function has a parameter which type
+// is either string or pointer to char and parent function format attribute
+// type does not match with calling function format attribute type.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc) {
+  assert(FDecl);
+
+  // Check if function has format attribute with forwarded format string.
+  IdentifierInfo *AttrType;
+  if (!llvm::any_of(
+  FDecl->specific_attrs(), [&](const FormatAttr *Attr) {
+if (!Args[Attr->getFirstArg()]->getReferencedDeclOfCallee())
+  return false;
+
+AttrType = Attr->getType();
+return true;
+  }))
+return;
+
+  const FunctionDecl *ParentFuncDecl = getCurFunctionDecl();
+  if (!ParentFuncDecl)
+return;
+
+  // Check if parent function has string or pointer to char parameter.
+  unsigned int StringIndex = 0;
+  if (!llvm::any_of(
+  ParentFuncDecl->parameters(), [&](const ParmVarDecl *Param) {
+StringIndex = Param->getFunctionScopeIndex() + 1;
+QualType Ty = Param->getType();
+if (isNSStringType(Ty, Context, true))
+  return true;
+if (isCFStringType(Ty, Context))
+  return true;
+if (Ty->isPointerType() &&
+Ty->castAs()
+->getPointeeType()
+->isSpecificBuiltinType(BuiltinType::Char_S))
+  return true;
+return false;
+  }))
+return;

budimirarandjelovicsyrmia wrote:

Covered mentioned example (forward_tgt) in 
clang/test/Sema/attr-format-missing.c:176-182 (function f20).

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2023-12-21 Thread Budimir Aranđelović via cfe-commits

https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70024

From c8d3fbdf7fc494aaf5c3ac1e2ca9545d8bc4e0bc Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia 
Date: Fri, 13 Oct 2023 14:45:15 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst   |   4 +-
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Sema/Sema.h   |   4 +
 clang/lib/Sema/SemaChecking.cpp   |   4 +-
 clang/lib/Sema/SemaDeclAttr.cpp   |  90 
 clang/test/Sema/attr-format-missing.c | 213 ++
 clang/test/Sema/attr-format-missing.cpp   | 132 +++
 8 files changed, 448 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9cc2a72f4c864d..1ca055f65f2115 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -65,7 +65,9 @@ Improvements to Clang's diagnostics
 ^^^
 - We now generate a diagnostic for signed integer overflow due to unary minus
   in a non-constant expression context. This fixes
-  `Issue 31643 `_
+  `Issue 31643 `_.
+- We now generate a diagnostic for missing format attributes
+  `Issue 60718 `_.
 
 Non-comprehensive list of changes in this release
 -
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 17fdcffa2d4274..114cbbc2e82b85 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -482,7 +482,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 6d6f474f6dcdab..964166b70c2aee 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -936,6 +936,9 @@ def err_opencl_invalid_param : Error<
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
 def warn_enum_value_overflow : Warning<"overflow in enumeration value">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 741c2503127af7..064506e7096033 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -10615,6 +10615,10 @@ class Sema final {
 ChangedStateAtExit
   };
 
+  void DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc);
+
   void DiagnoseNonDefaultPragmaAlignPack(PragmaAlignPackDiagnoseKind Kind,
  SourceLocation IncludeLoc);
   void DiagnoseUnterminatedPragmaAlignPack();
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 4602284309491c..d3ac6cb519c56e 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -6014,8 +6014,10 @@ void Sema::checkCall(NamedDecl *FDecl, const 
FunctionProtoType *Proto,
 }
   }
 
-  if (FD)
+  if (FD) {
 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
+DiagnoseMissingFormatAttributes(FD, Args, Range.getBegin());
+  }
 }
 
 /// CheckConstructorCall - Check a constructor call for correctness and safety
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 1a0bfb3d91bcc8..e2dfdd806f7c8b 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -6849,6 +6849,96 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, const 
ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// Check if parent function misses format attribute. If misses, emit warning.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+

[clang] [clang] Catch missing format attributes (PR #70024)

2023-12-10 Thread Aaron Puchert via cfe-commits


@@ -6849,6 +6849,73 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, const 
ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// Warn if parent function misses format attribute. Parent function misses
+// format attribute if there is an argument format string forwarded to calling
+// function with format attribute, parent function has a parameter which type
+// is either string or pointer to char and parent function format attribute
+// type does not match with calling function format attribute type.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc) {
+  assert(FDecl);
+
+  // Check if function has format attribute with forwarded format string.
+  IdentifierInfo *AttrType;
+  if (!llvm::any_of(
+  FDecl->specific_attrs(), [&](const FormatAttr *Attr) {
+if (!Args[Attr->getFirstArg()]->getReferencedDeclOfCallee())
+  return false;
+
+AttrType = Attr->getType();
+return true;
+  }))
+return;
+
+  const FunctionDecl *ParentFuncDecl = getCurFunctionDecl();
+  if (!ParentFuncDecl)
+return;
+
+  // Check if parent function has string or pointer to char parameter.
+  unsigned int StringIndex = 0;
+  if (!llvm::any_of(
+  ParentFuncDecl->parameters(), [&](const ParmVarDecl *Param) {
+StringIndex = Param->getFunctionScopeIndex() + 1;
+QualType Ty = Param->getType();
+if (isNSStringType(Ty, Context, true))
+  return true;
+if (isCFStringType(Ty, Context))
+  return true;
+if (Ty->isPointerType() &&
+Ty->castAs()
+->getPointeeType()
+->isSpecificBuiltinType(BuiltinType::Char_S))
+  return true;
+return false;
+  }))
+return;

aaronpuchert wrote:

Hmm, it seems that GCC is also warning in cases where we're not forwarding the 
format string, for example here:
```c
void forward_tgt(char* tgt) {
va_list va;
const char* fmt;
vsprintf(tgt, fmt, va);
}
```
produces "warning: function 'void forward_tgt(char*)' might be a candidate for 
'gnu_printf' format attribute [-Wsuggest-attribute=format]". But I think that's 
a false positive, and we can easily check for proper forwarding instead.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2023-12-10 Thread Aaron Puchert via cfe-commits


@@ -6849,6 +6849,73 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, const 
ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// Warn if parent function misses format attribute. Parent function misses
+// format attribute if there is an argument format string forwarded to calling
+// function with format attribute, parent function has a parameter which type
+// is either string or pointer to char and parent function format attribute
+// type does not match with calling function format attribute type.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc) {
+  assert(FDecl);
+
+  // Check if function has format attribute with forwarded format string.
+  IdentifierInfo *AttrType;
+  if (!llvm::any_of(
+  FDecl->specific_attrs(), [&](const FormatAttr *Attr) {
+if (!Args[Attr->getFirstArg()]->getReferencedDeclOfCallee())
+  return false;
+
+AttrType = Attr->getType();
+return true;
+  }))
+return;
+
+  const FunctionDecl *ParentFuncDecl = getCurFunctionDecl();
+  if (!ParentFuncDecl)
+return;
+
+  // Check if parent function has string or pointer to char parameter.
+  unsigned int StringIndex = 0;
+  if (!llvm::any_of(
+  ParentFuncDecl->parameters(), [&](const ParmVarDecl *Param) {
+StringIndex = Param->getFunctionScopeIndex() + 1;
+QualType Ty = Param->getType();
+if (isNSStringType(Ty, Context, true))
+  return true;
+if (isCFStringType(Ty, Context))
+  return true;
+if (Ty->isPointerType() &&
+Ty->castAs()
+->getPointeeType()
+->isSpecificBuiltinType(BuiltinType::Char_S))
+  return true;
+return false;
+  }))
+return;
+
+  // Check if there is parent function format attribute which type matches
+  // calling function format attribute type.
+  if (llvm::any_of(
+  ParentFuncDecl->specific_attrs(),
+  [&](const FormatAttr *Attr) { return Attr->getType() == AttrType; }))
+return;
+
+  unsigned int FirstToCheck =
+  ParentFuncDecl->isVariadic() ? (ParentFuncDecl->getNumParams() + 1) : 0;
+
+  // Diagnose missing format attributes
+  std::string InsertionText;
+  llvm::raw_string_ostream OS(InsertionText);
+  OS << "__attribute__((format(" << AttrType->getName() << ", "
+ << std::to_string(StringIndex) << ", " << std::to_string(FirstToCheck)

aaronpuchert wrote:

I'm surprised that we need `std::to_string` here, can 
`llvm::raw_string_ostream` not print integers directly?

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2023-12-10 Thread Aaron Puchert via cfe-commits


@@ -6849,6 +6849,73 @@ static void handleSwiftAsyncAttr(Sema , Decl *D, const 
ParsedAttr ) {
 checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// Warn if parent function misses format attribute. Parent function misses
+// format attribute if there is an argument format string forwarded to calling
+// function with format attribute, parent function has a parameter which type
+// is either string or pointer to char and parent function format attribute
+// type does not match with calling function format attribute type.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+   ArrayRef Args,
+   SourceLocation Loc) {
+  assert(FDecl);
+
+  // Check if function has format attribute with forwarded format string.
+  IdentifierInfo *AttrType;
+  if (!llvm::any_of(
+  FDecl->specific_attrs(), [&](const FormatAttr *Attr) {
+if (!Args[Attr->getFirstArg()]->getReferencedDeclOfCallee())
+  return false;
+
+AttrType = Attr->getType();
+return true;
+  }))
+return;
+
+  const FunctionDecl *ParentFuncDecl = getCurFunctionDecl();
+  if (!ParentFuncDecl)
+return;
+
+  // Check if parent function has string or pointer to char parameter.
+  unsigned int StringIndex = 0;
+  if (!llvm::any_of(
+  ParentFuncDecl->parameters(), [&](const ParmVarDecl *Param) {
+StringIndex = Param->getFunctionScopeIndex() + 1;
+QualType Ty = Param->getType();
+if (isNSStringType(Ty, Context, true))
+  return true;
+if (isCFStringType(Ty, Context))
+  return true;
+if (Ty->isPointerType() &&
+Ty->castAs()
+->getPointeeType()
+->isSpecificBuiltinType(BuiltinType::Char_S))
+  return true;
+return false;
+  }))
+return;

aaronpuchert wrote:

I don't think we can decide this based on the type. We should look at the 
argument and check if it's a `DeclRefExpr` referring to a parameter of the 
parent function. There can be other (unrelated) string arguments. (See also my 
comments in the tests.)

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2023-12-10 Thread Aaron Puchert via cfe-commits

https://github.com/aaronpuchert edited 
https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Catch missing format attributes (PR #70024)

2023-12-10 Thread Aaron Puchert via cfe-commits


@@ -0,0 +1,143 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+
+#include 
+#include 
+#include 
+#include 
+
+__attribute__((__format__ (__scanf__, 1, 4)))
+void f1(char *out, const size_t len, const char *format, ... /* args */)
+{
+va_list args;
+vsnprintf(out, len, format, args); // expected-warning {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f1'}}
+   // CHECK-FIXES: 
__attribute__((format(printf, 1, 4)))
+}
+
+void f2(char *out, va_list args)
+{
+vprintf(out, args); // expected-warning {{diagnostic behavior may be 
improved by adding the 'printf' format attribute to the declaration of 'f2'}}
+// CHECK-FIXES: __attribute__((format(printf, 1, 0)))
+vscanf(out, args); // expected-warning {{diagnostic behavior may be 
improved by adding the 'scanf' format attribute to the declaration of 'f2'}}
+   // CHECK-FIXES: __attribute__((format(scanf, 1, 0)))
+}
+
+void f3(char* out, ... /* args */)
+{
+va_list args;
+vprintf("test", args); // no warning
+}
+
+void f4(char *out, ... /* args */)
+{
+const char *ch;
+va_list args;
+vscanf(ch, args); // expected-warning {{diagnostic behavior may be 
improved by adding the 'scanf' format attribute to the declaration of 'f4'}}
+  // CHECK-FIXES: __attribute__((format(scanf, 1, 2)))

aaronpuchert wrote:

I don't think we can propagate the attribute here, since the format string is a 
local variable. It doesn't come from the caller.

The `out` parameter seems unused here, and has no relation to the format string 
`ch`.

https://github.com/llvm/llvm-project/pull/70024
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


  1   2   >