[clang] [libclc] [libcxx] [lld] [llvm] [openmp] llvm encode decode (PR #87187)

2024-04-07 Thread Yingchi Long via cfe-commits

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


[clang] [llvm] [Clang][BPF] Allow sign/zero extension for call parameters with int/uint types (PR #84874)

2024-03-12 Thread Yingchi Long via cfe-commits

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


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


[clang] [llvm] BPF address space insn (PR #84410)

2024-03-08 Thread Yingchi Long via cfe-commits

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


[clang] [llvm] BPF address space insn (PR #84410)

2024-03-08 Thread Yingchi Long via cfe-commits


@@ -0,0 +1,52 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 
UTC_ARGS: --version 4
+; RUN: opt --bpf-check-and-opt-ir -S -mtriple=bpf-pc-linux < %s | FileCheck %s
+
+; Generated from the following C code:
+;
+;   extern int __uptr *magic1();
+;   extern int __uptr *magic2();
+;
+;   void test(long i) {
+; int __uptr *a;
+;
+; if (i > 42)
+;   a = magic1();
+; else
+;   a = magic2();
+; a[5] = 7;
+;   }
+;
+; Using the following command:
+;
+;   clang --target=bpf -O2 -S -emit-llvm -o t.ll t.c
+
+; Function Attrs: nounwind

inclyc wrote:

No, it do has 'nounwind' attr because it is attributed with `#0`, however, in 
our testing ir I see `#0` is omitted

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


[clang] [llvm] BPF address space insn (PR #84410)

2024-03-08 Thread Yingchi Long via cfe-commits


@@ -31,6 +31,9 @@ def SDT_BPFMEMCPY   : SDTypeProfile<0, 4, [SDTCisVT<0, 
i64>,
SDTCisVT<1, i64>,
SDTCisVT<2, i64>,
SDTCisVT<3, i64>]>;
+def SDT_BPFAddrSpaceCast : SDTypeProfile<0, 3, [SDTCisPtrTy<0>,

inclyc wrote:

(question) Why not just use the common "addrspacecast" ISD node?

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


[clang] [llvm] BPF address space insn (PR #84410)

2024-03-08 Thread Yingchi Long via cfe-commits


@@ -0,0 +1,92 @@
+//=== BPFIRPeephole.cpp - IR Peephole Transformation 
--===//

inclyc wrote:

nit

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


[clang] [llvm] BPF address space insn (PR #84410)

2024-03-08 Thread Yingchi Long via cfe-commits


@@ -638,6 +643,31 @@ SDValue BPFTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue 
Op,
   return DAG.getMergeValues(Ops, SDLoc());
 }
 
+SDValue BPFTargetLowering::LowerADDRSPACECAST(SDValue Op,
+  SelectionDAG ) const {
+  auto *ACast = dyn_cast(Op);
+  const SDValue  = ACast->getOperand(0);
+  unsigned SrcAS = ACast->getSrcAddressSpace();
+  unsigned DstAS = ACast->getDestAddressSpace();
+  SDLoc DL(Op);
+
+  if (SrcAS > 0 && DstAS > 0) {
+SmallString<64> Msg;
+raw_svector_ostream OS(Msg);
+OS << "Can't cast address space " << SrcAS << " to " << DstAS
+   << ": either source or destination address space has to be zero";
+fail(DL, DAG, Msg);

inclyc wrote:

Our `fail` method is used for `DiagnosticInfoUnsupported` diagnostic, should we 
create a new diagnostic kind?

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


[clang] [llvm] BPF address space insn (PR #84410)

2024-03-08 Thread Yingchi Long via cfe-commits


@@ -0,0 +1,52 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 
UTC_ARGS: --version 4
+; RUN: opt --bpf-check-and-opt-ir -S -mtriple=bpf-pc-linux < %s | FileCheck %s
+
+; Generated from the following C code:
+;
+;   extern int __uptr *magic1();
+;   extern int __uptr *magic2();
+;
+;   void test(long i) {
+; int __uptr *a;
+;
+; if (i > 42)
+;   a = magic1();
+; else
+;   a = magic2();
+; a[5] = 7;
+;   }
+;
+; Using the following command:
+;
+;   clang --target=bpf -O2 -S -emit-llvm -o t.ll t.c
+
+; Function Attrs: nounwind

inclyc wrote:

this function attrs looks outdated

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


[clang] [clang][NFC] resolve redundant predicates (PR #79701)

2024-01-28 Thread Yingchi Long via cfe-commits

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


[clang] [clang] resolve redundant predicates (PR #79701)

2024-01-28 Thread Yingchi Long via cfe-commits

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

Looks like an NFC patch

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


[clang] [clang] resolve redundant predicates (PR #79701)

2024-01-28 Thread Yingchi Long via cfe-commits

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


[compiler-rt] [clang] [llvm] [mlir] [flang] [libc] [lldb] [clang-tools-extra] [libcxx] [AArch64] add intrinsic to generate a bfi instruction (PR #79672)

2024-01-28 Thread Yingchi Long via cfe-commits

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


[compiler-rt] [clang] [llvm] [mlir] [flang] [libc] [lldb] [clang-tools-extra] [libcxx] intrinsic to generate a bfi instruction (PR #79672)

2024-01-28 Thread Yingchi Long via cfe-commits


@@ -2558,6 +2558,14 @@ def : Pat<(rotr GPR32:$Rn, (i64 imm0_31:$imm)),
 def : Pat<(rotr GPR64:$Rn, (i64 imm0_63:$imm)),
   (EXTRXrri GPR64:$Rn, GPR64:$Rn, imm0_63:$imm)>;
 
+def SDT_AArch64BFI_32bit : SDTypeProfile<1, 2, [SDTCisVT<0, i32>, SDTCisVT<1, 
i32>,
+SDTCisVT<2, i32>]>;
+def SDT_AArch64BFI_64bit : SDTypeProfile<1, 2, [SDTCisVT<0, i64>, SDTCisVT<1, 
i64>,
+SDTCisVT<2, i64>]>;
+
+def aarch64_bfiw  : SDNode<"AArch64::BFMWri",  SDT_AArch64BFI_32bit>;

inclyc wrote:

(question) could you explain why creating a new SDNode? Is this necessary?

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


[mlir] [llvm] [libc] [clang] [libcxx] [lldb] [clang-tools-extra] [compiler-rt] [flang] intrinsic to generate a bfi instruction (PR #79672)

2024-01-28 Thread Yingchi Long via cfe-commits

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


[libc] [libcxx] [lldb] [clang] [compiler-rt] [clang-tools-extra] [mlir] [flang] [llvm] intrinsic to generate a bfi instruction (PR #79672)

2024-01-28 Thread Yingchi Long via cfe-commits

https://github.com/inclyc commented:

Also I kindly wonder if this intrinsic is necessary for optimization/codegen. 
Why not prefer inline assembly?

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


[flang] [compiler-rt] [llvm] [clang-tools-extra] [clang] [libcxxabi] [openmp] [mlir] [libcxx] [libc] [AArch64] Add custom lowering for load <3 x i8>. (PR #78632)

2024-01-26 Thread Yingchi Long via cfe-commits

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


[flang] [compiler-rt] [llvm] [clang-tools-extra] [clang] [libcxxabi] [openmp] [mlir] [libcxx] [libc] [AArch64] Add custom lowering for load <3 x i8>. (PR #78632)

2024-01-26 Thread Yingchi Long via cfe-commits


@@ -21248,6 +21297,51 @@ static SDValue foldTruncStoreOfExt(SelectionDAG , 
SDNode *N) {
   return SDValue();
 }
 
+// A custom combine to lower load <3 x i8> as the more efficient sequence
+// below:
+//ldrb wX, [x0, #2]
+//ldrh wY, [x0]
+//orr wX, wY, wX, lsl #16
+//fmov s0, wX
+//
+static SDValue combineV3I8LoadExt(LoadSDNode *LD, SelectionDAG ) {
+  EVT MemVT = LD->getMemoryVT();
+  if (MemVT != EVT::getVectorVT(*DAG.getContext(), MVT::i8, 3) ||
+  LD->getOriginalAlign() >= 4)
+return SDValue();
+
+  SDLoc DL(LD);
+  MachineFunction  = DAG.getMachineFunction();
+  SDValue Chain = LD->getChain();
+  SDValue BasePtr = LD->getBasePtr();
+  MachineMemOperand *MMO = LD->getMemOperand();
+  assert(LD->getOffset().isUndef() && "undef offset expected");

inclyc wrote:

> Could you share more details? 

I was writing some similar mechanics for `i64`, and assert that 
`LD->getOffset().isUndef()` while do custom lowering, and I could see 
miscompilation for SPEC 2017 test suite. It is very large and I'm trying to 
reduce it.

> We could, but unless we have a test case, I'd prefer to keep it as an assert 
> for now, to give us a chance to catch a test case, if it is possible.

Agreed, maybe just keep it as-is for now

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


[compiler-rt] [llvm] [libcxx] [clang] [mlir] [openmp] [libcxxabi] [flang] [clang-tools-extra] [libc] [AArch64] Add custom lowering for load <3 x i8>. (PR #78632)

2024-01-25 Thread Yingchi Long via cfe-commits


@@ -21248,6 +21297,51 @@ static SDValue foldTruncStoreOfExt(SelectionDAG , 
SDNode *N) {
   return SDValue();
 }
 
+// A custom combine to lower load <3 x i8> as the more efficient sequence
+// below:
+//ldrb wX, [x0, #2]
+//ldrh wY, [x0]
+//orr wX, wY, wX, lsl #16
+//fmov s0, wX
+//
+static SDValue combineV3I8LoadExt(LoadSDNode *LD, SelectionDAG ) {
+  EVT MemVT = LD->getMemoryVT();
+  if (MemVT != EVT::getVectorVT(*DAG.getContext(), MVT::i8, 3) ||
+  LD->getOriginalAlign() >= 4)
+return SDValue();
+
+  SDLoc DL(LD);
+  MachineFunction  = DAG.getMachineFunction();
+  SDValue Chain = LD->getChain();
+  SDValue BasePtr = LD->getBasePtr();
+  MachineMemOperand *MMO = LD->getMemOperand();
+  assert(LD->getOffset().isUndef() && "undef offset expected");

inclyc wrote:

I recently encountered a miscompilation when the program is relatively large, 
related to this assertion (not in aarch64 backend).

Should we use some if-branch to detect this, and return `SDValue()`?

I'm not sure how to construct indexed load directly in IR, perhaps I may make a 
reproducer from the test suite.

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


[compiler-rt] [libc] [libcxxabi] [flang] [libcxx] [clang] [openmp] [mlir] [clang-tools-extra] [llvm] [AArch64] Add custom lowering for load <3 x i8>. (PR #78632)

2024-01-22 Thread Yingchi Long via cfe-commits

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

LGTM because I just did similar thing in our (unfortunately, closed source) 
backend. Let's wait for aarch64 code owners :) !

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


[clang-tools-extra] [libc] [llvm] [clang] [flang] [libcxxabi] [compiler-rt] [mlir] [libcxx] [openmp] [AArch64] Add custom lowering for load <3 x i8>. (PR #78632)

2024-01-22 Thread Yingchi Long via cfe-commits


@@ -21095,6 +21095,50 @@ static SDValue foldTruncStoreOfExt(SelectionDAG , 
SDNode *N) {
   return SDValue();
 }
 
+// A custom combine to lower load <3 x i8> as the more efficient sequence
+// below:
+//ldrb wX, [x0, #2]
+//ldrh wY, [x0]
+//orr wX, wY, wX, lsl #16
+//fmov s0, wX
+//
+static SDValue combineV3I8LoadExt(LoadSDNode *LD, SelectionDAG ) {
+  EVT MemVT = LD->getMemoryVT();
+  if (MemVT != EVT::getVectorVT(*DAG.getContext(), MVT::i8, 3) ||
+  LD->getOriginalAlign() >= 4)
+return SDValue();
+
+  SDLoc DL(LD);
+  SDValue Chain = LD->getChain();
+  SDValue BasePtr = LD->getBasePtr();

inclyc wrote:

> assert to catch the use, if possible.

I think this makes sense because (as per my understanding) usually indexed 
loads are created while ISelDAGToDAG phase (right after ISelLowering), so I do 
not expect any assertion fails.

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


[clang] [clang] Parse attribute [[gnu::no_stack_protector]] (PR #75289)

2023-12-13 Thread Yingchi Long via cfe-commits

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


[clang] [clang] Parse attribute [[gnu::no_stack_protector]] (PR #75289)

2023-12-13 Thread Yingchi Long via cfe-commits

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


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


[clang] [clang] Parse attribute [[gnu::no_stack_protector]] (PR #75289)

2023-12-13 Thread Yingchi Long via cfe-commits

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


[clang] [clang] Parse attribute [[gnu::no_stack_protector]] (PR #75289)

2023-12-13 Thread Yingchi Long via cfe-commits


@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c23 %s

inclyc wrote:

PS. for cxx attribute you may create a new file.

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


[clang] [clang] Parse attribute [[gnu::no_stack_protector]] (PR #75289)

2023-12-13 Thread Yingchi Long via cfe-commits


@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c23 %s

inclyc wrote:

I just noticed that we already have test file at 
`clang/test/Sema/no_stack_protector.c`, may be just update that file.

> No other single attribute has its dedicated test file under the test 
> directory.

No, just see `no_stack_protector.c`.

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


[clang] [clang] Parse attribute [[gnu::no_stack_protector]] (PR #75289)

2023-12-13 Thread Yingchi Long via cfe-commits

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


[clang] [clang] Parse attribute [[gnu::no_stack_protector]] (PR #75289)

2023-12-13 Thread Yingchi Long via cfe-commits


@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c23 %s

inclyc wrote:

the file name `gnu-attributes.c` is too "generic", perhaps better place these 
files under `clang/test/Sema/attr-no-stack-protector.c` and 
`clang/test/SemaCXX/attr-no-stack-protector.c`

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


[clang] [clang] Parse attribute [[gnu::no_stack_protector]] (PR #75289)

2023-12-13 Thread Yingchi Long via cfe-commits

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


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


[clang] [clang] Parse attribute [[gnu::no_stack_protector]] (PR #75289)

2023-12-13 Thread Yingchi Long via cfe-commits

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

The PR contains functional change so please add test & assertions under 
`clang/test`

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


[clang] [Cygwin] Reduced number of inline elements of CallArgList. (PR #74977)

2023-12-11 Thread Yingchi Long via cfe-commits

inclyc wrote:

> I have build scripts and patches at: https://github.com/xu-chiheng/Note
> […](#)
> On Mon, Dec 11, 2023 at 11:25 PM Yingchi Long ***@***.***> wrote: When 
> bootstrapping Clang on Cygwin, there are various weird problems. What are 
> those "weired problems" Reducing the size of CallArgList solve them all. And 
> why reduce the size of CallArgList solve them all? Could you kindly 
> elaborate? — Reply to this email directly, view it on GitHub <[#74977 
> (comment)](https://github.com/llvm/llvm-project/pull/74977#issuecomment-1850302699)>,
>  or unsubscribe 
> 
>  . You are receiving this because you authored the thread.Message ID: 
> ***@***.***>

I see, can you just paste logs(e.g. compiler warnings) here or provide a small 
reproducer? (e.g step-by-step instructions to reproduce the issue).

I think it will be nice if the patch really improves llvm portability, but it 
is hard to reproduce bugs via a set of undocumented scripts & unknown build 
environments.

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


[clang] [Cygwin] Reduced number of inline elements of CallArgList. (PR #74977)

2023-12-11 Thread Yingchi Long via cfe-commits

inclyc wrote:

> When bootstrapping Clang on Cygwin, there are various weird problems.

What are those "weired problems"

> Reducing the size of CallArgList solve them all.

And why reduce the size of `CallArgList` solve them all? Could you kindly 
elaborate?

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


[clang] [Cygwin] Reduced number of inline elements of CallArgList. (PR #74977)

2023-12-10 Thread Yingchi Long via cfe-commits

https://github.com/inclyc commented:

> This fix bootstraping on Cygwin, using GCC 13.2.0 as stage 0 compiler.
It seems that the size of CallArgList can't exceed an unknown limit.

How about paste some logs here? This change seems to be completely NFC, and I 
suppose it shall work for GCC 13?

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


[clang] [Cygwin] Cygwin macro (PR #74973)

2023-12-10 Thread Yingchi Long via cfe-commits

https://github.com/inclyc commented:

Would you please add some tests about your changes?

Also maybe it will be more useful to write some detailed descriptions, so 
others can understand your code easily. Cheers!

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


[clang] [llvm] [Sema] Implement support for -Wformat-signedness (PR #74440)

2023-12-05 Thread Yingchi Long via cfe-commits

https://github.com/inclyc commented:

Also please add release notes to let users know what has been changed :)

See: `clang/docs/ReleaseNotes.rst`

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


[clang] [llvm] [Sema] Implement support for -Wformat-signedness (PR #74440)

2023-12-05 Thread Yingchi Long via cfe-commits


@@ -918,6 +918,9 @@ def Wdeprecated : Flag<["-"], "Wdeprecated">, 
Group,
   HelpText<"Enable warnings for deprecated constructs and define 
__DEPRECATED">;
 def Wno_deprecated : Flag<["-"], "Wno-deprecated">, Group,
   Visibility<[ClangOption, CC1Option]>;
+def Wformat_signedness : Flag<["-"], "Wformat-signedness">,

inclyc wrote:

I agree with @hazohelet 

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


[clang] [llvm] [Sema] Implement support for -Wformat-signedness (PR #74440)

2023-12-05 Thread Yingchi Long via cfe-commits


@@ -0,0 +1,107 @@
+// RUN: %clang_cc1 -std=c11 -fsyntax-only -verify -Wformat -Wformat-signedness 
%s
+
+int printf(const char *restrict format, ...);
+int scanf(const char * restrict, ...);
+
+void test_printf_bool(_Bool x)
+{
+printf("%d", x); // no-warning
+printf("%u", x); // expected-warning{{format specifies type 'unsigned int' 
but the argument has type '_Bool'}}
+printf("%x", x); // expected-warning{{format specifies type 'unsigned int' 
but the argument has type '_Bool'}}
+}
+
+void test_printf_char(char x)
+{
+printf("%c", x); // no-warning
+}
+
+void test_printf_unsigned_char(unsigned char x)
+{
+printf("%c", x); // no-warning
+}
+
+void test_printf_int(int x)
+{
+printf("%d", x); // no-warning
+printf("%u", x); // expected-warning{{format specifies type 'unsigned int' 
but the argument has type 'int'}}
+printf("%x", x); // expected-warning{{format specifies type 'unsigned int' 
but the argument has type 'int'}}

inclyc wrote:

will this be a little pedantic?

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


[clang] [llvm] [Sema] Implement support for -Wformat-signedness (PR #74440)

2023-12-05 Thread Yingchi Long via cfe-commits


@@ -11815,6 +11815,18 @@ isArithmeticArgumentPromotion(Sema , const 
ImplicitCastExpr *ICE) {
  S.Context.getFloatingTypeOrder(From, To) < 0;
 }
 
+static clang::analyze_format_string::ArgType::MatchKind

inclyc wrote:

```suggestion
static analyze_format_string::ArgType::MatchKind
```

Maybe we can remove this namespace?

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


[clang] 596f76a - Revert "[C2x] reject type definitions in offsetof"

2023-01-16 Thread Yingchi Long via cfe-commits

Author: Yingchi Long
Date: 2023-01-16T16:52:50+08:00
New Revision: 596f76a799c933927eec4d8ac8a83c44efff9854

URL: 
https://github.com/llvm/llvm-project/commit/596f76a799c933927eec4d8ac8a83c44efff9854
DIFF: 
https://github.com/llvm/llvm-project/commit/596f76a799c933927eec4d8ac8a83c44efff9854.diff

LOG: Revert "[C2x] reject type definitions in offsetof"

This reverts commit e327b52766ed497e4779f4e652b9ad237dfda8e6.

Added: 


Modified: 
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/include/clang/Parse/Parser.h
clang/include/clang/Parse/RAIIObjectsForParser.h
clang/include/clang/Sema/Sema.h
clang/lib/Parse/ParseDecl.cpp
clang/lib/Parse/ParseDeclCXX.cpp
clang/lib/Parse/ParseExpr.cpp
clang/lib/Sema/SemaDecl.cpp
clang/lib/Sema/SemaDeclCXX.cpp
clang/lib/Sema/SemaTemplate.cpp
clang/test/C/drs/dr4xx.c
clang/test/Parser/declarators.c
clang/test/SemaCXX/offsetof.cpp

Removed: 
clang/test/C/C2x/n2350.c



diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a75bc1df2d7c9..09133f967f01f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -670,9 +670,6 @@ C2x Feature Support
   va_start(list); // Invalid in C17 and earlier, valid in C2x and later.
   va_end(list);
 }
-
-- Reject type definitions in the ``type`` argument of ``__builtin_offsetof`` 
-  according to `WG14 N2350 
`_.
 
 C++ Language Changes in Clang
 -

diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 862ac845bda45..02afb098b2395 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1650,8 +1650,6 @@ def err_type_defined_in_condition : Error<
   "%0 cannot be defined in a condition">;
 def err_type_defined_in_enum : Error<
   "%0 cannot be defined in an enumeration">;
-def err_type_defined_in_offsetof : Error<
-  "%0 cannot be defined in '%select{__builtin_offsetof|offsetof}1'">;
 
 def note_pure_virtual_function : Note<
   "unimplemented pure virtual method %0 in %1">;

diff  --git a/clang/include/clang/Parse/Parser.h 
b/clang/include/clang/Parse/Parser.h
index 6f9581b9ea1fc..7a33532eec14e 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -62,7 +62,6 @@ class Parser : public CodeCompletionHandler {
   friend class ColonProtectionRAIIObject;
   friend class ParsingOpenMPDirectiveRAII;
   friend class InMessageExpressionRAIIObject;
-  friend class OffsetOfStateRAIIObject;
   friend class PoisonSEHIdentifiersRAIIObject;
   friend class ObjCDeclContextSwitch;
   friend class ParenBraceBracketBalancer;
@@ -249,8 +248,6 @@ class Parser : public CodeCompletionHandler {
   /// function call.
   bool CalledSignatureHelp = false;
 
-  Sema::OffsetOfKind OffsetOfState = Sema::OffsetOfKind::OOK_Outside;
-
   /// The "depth" of the template parameters currently being parsed.
   unsigned TemplateParameterDepth;
 

diff  --git a/clang/include/clang/Parse/RAIIObjectsForParser.h 
b/clang/include/clang/Parse/RAIIObjectsForParser.h
index cb525c9d0edd6..5ae609e600734 100644
--- a/clang/include/clang/Parse/RAIIObjectsForParser.h
+++ b/clang/include/clang/Parse/RAIIObjectsForParser.h
@@ -341,19 +341,6 @@ namespace clang {
 }
   };
 
-  class OffsetOfStateRAIIObject {
-Sema::OffsetOfKind 
-Sema::OffsetOfKind OldValue;
-
-  public:
-OffsetOfStateRAIIObject(Parser , Sema::OffsetOfKind Value)
-: OffsetOfState(P.OffsetOfState), OldValue(P.OffsetOfState) {
-  OffsetOfState = Value;
-}
-
-~OffsetOfStateRAIIObject() { OffsetOfState = OldValue; }
-  };
-
   /// RAII object that makes sure paren/bracket/brace count is correct
   /// after declaration/statement parsing, even when there's a parsing error.
   class ParenBraceBracketBalancer {

diff  --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 35e319879a98d..30c5ea608f7a0 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3304,16 +3304,6 @@ class Sema final {
 TUK_Friend   // Friend declaration:  'friend struct foo;'
   };
 
-  enum OffsetOfKind {
-// Not parsing a type within __builtin_offsetof.
-OOK_Outside,
-// Parsing a type within __builtin_offsetof.
-OOK_Builtin,
-// Parsing a type within macro "offsetof", defined in __buitin_offsetof
-// To improve our diagnostic message.
-OOK_Macro,
-  };
-
   Decl *ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
  SourceLocation KWLoc, CXXScopeSpec , IdentifierInfo *Name,
  SourceLocation NameLoc, const ParsedAttributesView ,
@@ -3322,7 +3312,7 @@ class Sema final {
  bool , SourceLocation 

[clang] e327b52 - [C2x] reject type definitions in offsetof

2023-01-12 Thread Yingchi Long via cfe-commits

Author: Yingchi Long
Date: 2023-01-13T15:33:26+08:00
New Revision: e327b52766ed497e4779f4e652b9ad237dfda8e6

URL: 
https://github.com/llvm/llvm-project/commit/e327b52766ed497e4779f4e652b9ad237dfda8e6
DIFF: 
https://github.com/llvm/llvm-project/commit/e327b52766ed497e4779f4e652b9ad237dfda8e6.diff

LOG: [C2x] reject type definitions in offsetof

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm made very
clear that it is an UB having type definitions with in offsetof. After
this patch clang will reject any type definitions in __builtin_offsetof.

Fixes https://github.com/llvm/llvm-project/issues/57065

```
local/offsetof.c:10:38: error: 'struct S' cannot be defined in 
'__builtin_offsetof'
return __builtin_offsetof(struct S{ int a, b;}, a);
 ^
```

Reviewed By: aaron.ballman, #clang-language-wg

Differential Revision: https://reviews.llvm.org/D133574

Added: 
clang/test/C/C2x/n2350.c

Modified: 
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/include/clang/Parse/Parser.h
clang/include/clang/Parse/RAIIObjectsForParser.h
clang/include/clang/Sema/Sema.h
clang/lib/Parse/ParseDecl.cpp
clang/lib/Parse/ParseDeclCXX.cpp
clang/lib/Parse/ParseExpr.cpp
clang/lib/Sema/SemaDecl.cpp
clang/lib/Sema/SemaDeclCXX.cpp
clang/lib/Sema/SemaTemplate.cpp
clang/test/C/drs/dr4xx.c
clang/test/Parser/declarators.c
clang/test/SemaCXX/offsetof.cpp

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 65a9d65715ddd..c5626ac8fe272 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -668,6 +668,9 @@ C2x Feature Support
   va_start(list); // Invalid in C17 and earlier, valid in C2x and later.
   va_end(list);
 }
+
+- Reject type definitions in the ``type`` argument of ``__builtin_offsetof`` 
+  according to `WG14 N2350 
`_.
 
 C++ Language Changes in Clang
 -

diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 02afb098b2395..862ac845bda45 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1650,6 +1650,8 @@ def err_type_defined_in_condition : Error<
   "%0 cannot be defined in a condition">;
 def err_type_defined_in_enum : Error<
   "%0 cannot be defined in an enumeration">;
+def err_type_defined_in_offsetof : Error<
+  "%0 cannot be defined in '%select{__builtin_offsetof|offsetof}1'">;
 
 def note_pure_virtual_function : Note<
   "unimplemented pure virtual method %0 in %1">;

diff  --git a/clang/include/clang/Parse/Parser.h 
b/clang/include/clang/Parse/Parser.h
index 67e163ca512d1..8d934d59a580b 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -61,6 +61,7 @@ class Parser : public CodeCompletionHandler {
   friend class ColonProtectionRAIIObject;
   friend class ParsingOpenMPDirectiveRAII;
   friend class InMessageExpressionRAIIObject;
+  friend class OffsetOfStateRAIIObject;
   friend class PoisonSEHIdentifiersRAIIObject;
   friend class ObjCDeclContextSwitch;
   friend class ParenBraceBracketBalancer;
@@ -247,6 +248,8 @@ class Parser : public CodeCompletionHandler {
   /// function call.
   bool CalledSignatureHelp = false;
 
+  Sema::OffsetOfKind OffsetOfState = Sema::OffsetOfKind::OOK_Outside;
+
   /// The "depth" of the template parameters currently being parsed.
   unsigned TemplateParameterDepth;
 

diff  --git a/clang/include/clang/Parse/RAIIObjectsForParser.h 
b/clang/include/clang/Parse/RAIIObjectsForParser.h
index 5ae609e600734..cb525c9d0edd6 100644
--- a/clang/include/clang/Parse/RAIIObjectsForParser.h
+++ b/clang/include/clang/Parse/RAIIObjectsForParser.h
@@ -341,6 +341,19 @@ namespace clang {
 }
   };
 
+  class OffsetOfStateRAIIObject {
+Sema::OffsetOfKind 
+Sema::OffsetOfKind OldValue;
+
+  public:
+OffsetOfStateRAIIObject(Parser , Sema::OffsetOfKind Value)
+: OffsetOfState(P.OffsetOfState), OldValue(P.OffsetOfState) {
+  OffsetOfState = Value;
+}
+
+~OffsetOfStateRAIIObject() { OffsetOfState = OldValue; }
+  };
+
   /// RAII object that makes sure paren/bracket/brace count is correct
   /// after declaration/statement parsing, even when there's a parsing error.
   class ParenBraceBracketBalancer {

diff  --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index fd60a1f9ac43e..be5265b7968b8 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3304,6 +3304,16 @@ class Sema final {
 TUK_Friend   // Friend declaration:  'friend struct foo;'
   };
 
+  enum OffsetOfKind {
+// Not parsing a type within __builtin_offsetof.
+OOK_Outside,
+// Parsing a 

[clang] 2ec79af - [Sema] check InitListExpr format strings like {"foo"}

2022-11-22 Thread Yingchi Long via cfe-commits

Author: Yingchi Long
Date: 2022-11-23T04:58:19+08:00
New Revision: 2ec79afd89932f0d5a9c44050e7b6dc08ff51699

URL: 
https://github.com/llvm/llvm-project/commit/2ec79afd89932f0d5a9c44050e7b6dc08ff51699
DIFF: 
https://github.com/llvm/llvm-project/commit/2ec79afd89932f0d5a9c44050e7b6dc08ff51699.diff

LOG: [Sema] check InitListExpr format strings like {"foo"}

Adds InitListExpr case in format string checks.

e.g.

int sprintf(char *__restrict, const char * __restrict, ...);

int foo()
{
char data[100];
constexpr const char* fmt2{"%d"};  // no-warning
sprintf(data, fmt2, 123);
}

Fixes: https://github.com/llvm/llvm-project/issues/58900

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D137839

Added: 


Modified: 
clang/lib/Sema/SemaChecking.cpp
clang/test/SemaCXX/format-strings.cpp

Removed: 




diff  --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 75bd0ef57bcc0..7b6279baf8eed 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8585,6 +8585,15 @@ checkFormatStringExpr(Sema , const Expr *E, 
ArrayRef Args,
 return SLCT_UncheckedLiteral;
 
   switch (E->getStmtClass()) {
+  case Stmt::InitListExprClass:
+// Handle expressions like {"foobar"}.
+if (const clang::Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) {
+  return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg,
+   Type, CallType, /*InFunctionCall*/ false,
+   CheckedVarArgs, UncoveredArg, Offset,
+   IgnoreStringsWithoutSpecifiers);
+}
+return SLCT_NotALiteral;
   case Stmt::BinaryConditionalOperatorClass:
   case Stmt::ConditionalOperatorClass: {
 // The expression is a literal if both sub-expressions were, and it was

diff  --git a/clang/test/SemaCXX/format-strings.cpp 
b/clang/test/SemaCXX/format-strings.cpp
index 2378eea9b3c07..2def986bdf9f6 100644
--- a/clang/test/SemaCXX/format-strings.cpp
+++ b/clang/test/SemaCXX/format-strings.cpp
@@ -203,6 +203,12 @@ void f() {
   printf(string_linebreak(), 1, 2, 3, 4); // expected-warning {{format 
specifies type 'char *' but the argument has type 'int'}}
   printf(not_literal(), 1, 2, 3, 4); // expected-warning {{format string is 
not a string literal}}
   printf(wrap_constexpr(), 1, 2); // expected-warning {{format specifies type 
'char *' but the argument has type 'int'}}
+
+  constexpr const char *fmt {"%d%d"};
+  printf(fmt, 1, 1); // no-warning
+
+  constexpr const char *fmt2 {"%d"}; // expected-note{{format string is 
defined here}}
+  printf(fmt2, "oops"); // expected-warning{{format specifies type 'int' but 
the argument has type}}
 }
 
 



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 0e40a78 - [clang][NFC] sync comments from declaration of InitializePreprocessor

2022-10-29 Thread YingChi Long via cfe-commits

Author: YingChi Long
Date: 2022-10-29T14:23:03+08:00
New Revision: 0e40a78319232ff22dcc54dcb92bfef8495325f1

URL: 
https://github.com/llvm/llvm-project/commit/0e40a78319232ff22dcc54dcb92bfef8495325f1
DIFF: 
https://github.com/llvm/llvm-project/commit/0e40a78319232ff22dcc54dcb92bfef8495325f1.diff

LOG: [clang][NFC] sync comments from declaration of InitializePreprocessor

Added: 


Modified: 
clang/lib/Frontend/InitPreprocessor.cpp

Removed: 




diff  --git a/clang/lib/Frontend/InitPreprocessor.cpp 
b/clang/lib/Frontend/InitPreprocessor.cpp
index 96b93dcdf044..8e0a42c64479 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -1305,8 +1305,7 @@ static void InitializePredefinedMacros(const TargetInfo 
,
 }
 
 /// InitializePreprocessor - Initialize the preprocessor getting it and the
-/// environment ready to process a single file. This returns true on error.
-///
+/// environment ready to process a single file.
 void clang::InitializePreprocessor(
 Preprocessor , const PreprocessorOptions ,
 const PCHContainerReader ,



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 94e8bd0 - [clang] Fix crash upon stray coloncolon token in C2x mode

2022-10-18 Thread YingChi Long via cfe-commits

Author: Jialun Hu
Date: 2022-10-18T21:57:32+08:00
New Revision: 94e8bd002c81ace308ddac9b66385ef932655fd3

URL: 
https://github.com/llvm/llvm-project/commit/94e8bd002c81ace308ddac9b66385ef932655fd3
DIFF: 
https://github.com/llvm/llvm-project/commit/94e8bd002c81ace308ddac9b66385ef932655fd3.diff

LOG: [clang] Fix crash upon stray coloncolon token in C2x mode

The parser assumes that the lexer never emits coloncolon token for C code, but 
this assumption no longer holds in C2x attribute namespaces. As a result, stray 
coloncolon tokens out of attributes cause assertion failures and hangs in 
release build, which this patch tries to handle.

Crash input minimal example: `T n::v`

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D133248

Added: 


Modified: 
clang/docs/ReleaseNotes.rst
clang/include/clang/Parse/Parser.h
clang/lib/Parse/ParseDecl.cpp
clang/lib/Parse/Parser.cpp
clang/test/Parser/c2x-attributes.c

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6eaec2a5f3e9f..9fe63dd5de840 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -251,6 +251,7 @@ Bug Fixes
 - Address the thread identification problems in coroutines.
   `Issue 47177 `_
   `Issue 47179 `_
+- Fix a crash upon stray coloncolon token in C2x mode.
 
 Improvements to Clang's diagnostics
 ^^^

diff  --git a/clang/include/clang/Parse/Parser.h 
b/clang/include/clang/Parse/Parser.h
index c518ce5487bd8..d70f36daa891f 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -866,10 +866,11 @@ class Parser : public CodeCompletionHandler {
   bool TryAnnotateCXXScopeToken(bool EnteringContext = false);
 
   bool MightBeCXXScopeToken() {
-return Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
-   (Tok.is(tok::annot_template_id) &&
-NextToken().is(tok::coloncolon)) ||
-   Tok.is(tok::kw_decltype) || Tok.is(tok::kw___super);
+return getLangOpts().CPlusPlus &&
+   (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
+(Tok.is(tok::annot_template_id) &&
+ NextToken().is(tok::coloncolon)) ||
+Tok.is(tok::kw_decltype) || Tok.is(tok::kw___super));
   }
   bool TryAnnotateOptionalCXXScopeToken(bool EnteringContext = false) {
 return MightBeCXXScopeToken() && TryAnnotateCXXScopeToken(EnteringContext);

diff  --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index ddb83a8c6694d..e15a6ffa8d689 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -5411,6 +5411,8 @@ bool Parser::isDeclarationSpecifier(
 return isDeclarationSpecifier(AllowImplicitTypename);
 
   case tok::coloncolon:   // ::foo::bar
+if (!getLangOpts().CPlusPlus)
+  return false;
 if (NextToken().is(tok::kw_new) ||// ::new
 NextToken().is(tok::kw_delete))   // ::delete
   return false;

diff  --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index 79528d25be21f..8fcd02146ba5c 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -2080,9 +2080,9 @@ bool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec(
 }
 
 if (!getLangOpts().CPlusPlus) {
-  // If we're in C, we can't have :: tokens at all (the lexer won't return
-  // them).  If the identifier is not a type, then it can't be scope 
either,
-  // just early exit.
+  // If we're in C, the only place we can have :: tokens is C2x
+  // attribute which is parsed elsewhere. If the identifier is not a type,
+  // then it can't be scope either, just early exit.
   return false;
 }
 

diff  --git a/clang/test/Parser/c2x-attributes.c 
b/clang/test/Parser/c2x-attributes.c
index f8ed1eda14886..e3201e2315d2f 100644
--- a/clang/test/Parser/c2x-attributes.c
+++ b/clang/test/Parser/c2x-attributes.c
@@ -141,3 +141,6 @@ void test_asm(void) {
 struct [[]] S4 *s; // expected-error {{an attribute list cannot appear here}}
 struct S5 {};
 int c = sizeof(struct [[]] S5); // expected-error {{an attribute list cannot 
appear here}}
+
+// Ensure that '::' outside of attributes does not crash and is not treated as 
scope
+double n::v; // expected-error {{expected ';' after top level declarator}}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 5ee51e8 - [clang] trim trailing space in format tests. NFC

2022-08-31 Thread YingChi Long via cfe-commits

Author: YingChi Long
Date: 2022-09-01T13:30:40+08:00
New Revision: 5ee51e8154253088b333c8e82c64d0f00d7d0524

URL: 
https://github.com/llvm/llvm-project/commit/5ee51e8154253088b333c8e82c64d0f00d7d0524
DIFF: 
https://github.com/llvm/llvm-project/commit/5ee51e8154253088b333c8e82c64d0f00d7d0524.diff

LOG: [clang] trim trailing space in format tests. NFC

Found in https://reviews.llvm.org/D132568

Differential Revision: https://reviews.llvm.org/D133085

Added: 


Modified: 
clang/test/FixIt/format.m
clang/test/FixIt/format.mm
clang/test/Sema/format-strings-scanf.c

Removed: 




diff  --git a/clang/test/FixIt/format.m b/clang/test/FixIt/format.m
index 966cf79b2e0f7..950765bad9339 100644
--- a/clang/test/FixIt/format.m
+++ b/clang/test/FixIt/format.m
@@ -37,7 +37,7 @@ void test_string_correction (char *x) {
   // CHECK: fix-it:"{{.*}}":{34:11-34:13}:"%s"
 }
 
-void test_object_correction (id x) {  
+void test_object_correction (id x) {
   NSLog(@"%d", x); // expected-warning{{format specifies type 'int' but the 
argument has type 'id'}}
   NSLog(@"%s", x); // expected-warning{{format specifies type 'char *' but the 
argument has type 'id'}}
   NSLog(@"%lf", x); // expected-warning{{format specifies type 'double' but 
the argument has type 'id'}}
@@ -108,7 +108,7 @@ void test_char(char c, signed char s, unsigned char u, 
uint8_t n) {
   NSLog(@"%c", c); // no-warning
   // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"
 
-  
+
   NSLog(@"%s", s); // expected-warning{{format specifies type 'char *' but the 
argument has type 'signed char'}}
   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"
 
@@ -197,11 +197,11 @@ void test_percent_C(void) {
   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unsigned short)"
 
   typedef unsigned short unichar;
-  
+
   NSLog(@"%C", 0x260300);  // expected-warning{{format specifies type 
'unichar' (aka 'unsigned short') but the argument has type 'int'}}
   // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%d"
   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unichar)"
-  
+
   NSLog(@"%C", data ? 0x2F : 0x260300); // expected-warning{{format 
specifies type 'unichar' (aka 'unsigned short') but the argument has type 
'int'}}
   // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%d"
   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unichar)("
@@ -239,7 +239,7 @@ void testSizeTypes(void) {
 
   printf("%zd", 0.f); // expected-warning-re{{format specifies type 'ssize_t' 
(aka '{{.+}}') but the argument has type 'float'}}
   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%f"
-  
+
   short x;
 #if !defined(__ANDROID__) && !defined(__Fuchsia__)
   printf("%zn", ); // expected-warning-re{{format specifies type 'ssize_t *' 
(aka '{{.+}}') but the argument has type 'short *'}}
@@ -266,7 +266,7 @@ void testPtrDiffTypes(void) {
 
   printf("%tu", 0.f); // expected-warning-re{{format specifies type 'unsigned 
ptr
diff _t' (aka '{{.+}}') but the argument has type 'float'}}
   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%f"
-  
+
   ptr
diff _t p2 = 0;
   printf("%td", p2);  // No warning.
 

diff  --git a/clang/test/FixIt/format.mm b/clang/test/FixIt/format.mm
index 9d89c062faa97..eed7c981f2c45 100644
--- a/clang/test/FixIt/format.mm
+++ b/clang/test/FixIt/format.mm
@@ -19,11 +19,11 @@ void test_percent_C() {
 
   NSLog(@"%C", wchar_data);  // expected-warning{{format specifies type 
'unichar' (aka 'unsigned short') but the argument has type 'wchar_t'}}
   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"(unichar)"
-  
+
   NSLog(@"%C", 0x260300);  // expected-warning{{format specifies type 
'unichar' (aka 'unsigned short') but the argument has type 'int'}}
   // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%d"
   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unichar)"
-  
+
   NSLog(@"%C", 0.0); // expected-warning{{format specifies type 'unichar' (aka 
'unsigned short') but the argument has type 'double'}}
   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%f"
   // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unichar)"

diff  --git a/clang/test/Sema/format-strings-scanf.c 
b/clang/test/Sema/format-strings-scanf.c
index 054439ecaf5a7..aebb68c37fb98 100644
--- a/clang/test/Sema/format-strings-scanf.c
+++ b/clang/test/Sema/format-strings-scanf.c
@@ -12,7 +12,7 @@ typedef __SIZE_TYPE__ size_t;
   unsigned int : (int)0,   
\
   unsigned short : (short)0,   
\
   unsigned char : (signed char)0))
-typedef __SSIZE_TYPE__ ssize_t; 
+typedef __SSIZE_TYPE__ ssize_t;
 
 typedef __PTRDIFF_TYPE__ ptr
diff _t;
 #define __UNSIGNED_PTRDIFF_TYPE__ 

[clang] e3bd67e - [clang][Sema] check default argument promotions for printf

2022-08-31 Thread YingChi Long via cfe-commits

Author: YingChi Long
Date: 2022-09-01T10:10:10+08:00
New Revision: e3bd67eddf65b20956513e91715b1f997dae2690

URL: 
https://github.com/llvm/llvm-project/commit/e3bd67eddf65b20956513e91715b1f997dae2690
DIFF: 
https://github.com/llvm/llvm-project/commit/e3bd67eddf65b20956513e91715b1f997dae2690.diff

LOG: [clang][Sema] check default argument promotions for printf

The main focus of this patch is to make ArgType::matchesType check for
possible default parameter promotions when the argType is not a pointer.
If so, no warning will be given for `int`, `unsigned int` types as
corresponding arguments to %hhd and %hd. However, the usage of %hhd
corresponding to short is relatively rare, and it is more likely to be a
misuse. This patch keeps the original behavior of clang like this as
much as possible, while making it more convenient to consider the
default arguments promotion.

Fixes https://github.com/llvm/llvm-project/issues/57102

Reviewed By: aaron.ballman, nickdesaulniers, #clang-language-wg

Differential Revision: https://reviews.llvm.org/D132568

Added: 


Modified: 
clang/docs/ReleaseNotes.rst
clang/include/clang/AST/FormatString.h
clang/lib/AST/FormatString.cpp
clang/lib/Sema/SemaChecking.cpp
clang/test/Sema/format-strings-freebsd.c
clang/test/Sema/format-strings-scanf.c
clang/test/Sema/format-strings.c
clang/www/c_status.html

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2251993a7c72b..bb7c508001db4 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -175,6 +175,10 @@ AIX Support
 C Language Changes in Clang
 ---
 
+- Adjusted ``-Wformat`` warnings according to `WG14 N2562 
`_.
+  Clang will now consider default argument promotions in printf, and remove 
unnecessary warnings.
+  Especially ``int`` argument with specifier ``%hhd`` and ``%hd``.
+
 C2x Feature Support
 ---
 

diff  --git a/clang/include/clang/AST/FormatString.h 
b/clang/include/clang/AST/FormatString.h
index 0f1f6b1340ff1..a2b8122890b92 100644
--- a/clang/include/clang/AST/FormatString.h
+++ b/clang/include/clang/AST/FormatString.h
@@ -261,8 +261,14 @@ class ArgType {
 /// instance, "%d" and float.
 NoMatch = 0,
 /// The conversion specifier and the argument type are compatible. For
-/// instance, "%d" and _Bool.
+/// instance, "%d" and int.
 Match = 1,
+/// The conversion specifier and the argument type are compatible because 
of
+/// default argument promotions. For instance, "%hhd" and int.
+MatchPromotion,
+/// The conversion specifier and the argument type are compatible but still
+/// seems likely to be an error. For instanace, "%hhd" and short.
+NoMatchPromotionTypeConfusion,
 /// The conversion specifier and the argument type are disallowed by the C
 /// standard, but are in practice harmless. For instance, "%p" and int*.
 NoMatchPedantic,

diff  --git a/clang/lib/AST/FormatString.cpp b/clang/lib/AST/FormatString.cpp
index cf41228ba2f09..83846438a05f5 100644
--- a/clang/lib/AST/FormatString.cpp
+++ b/clang/lib/AST/FormatString.cpp
@@ -348,7 +348,7 @@ ArgType::matchesType(ASTContext , QualType argTy) const {
   return Match;
 
 case AnyCharTy: {
-  if (const EnumType *ETy = argTy->getAs()) {
+  if (const auto *ETy = argTy->getAs()) {
 // If the enum is incomplete we know nothing about the underlying type.
 // Assume that it's 'int'.
 if (!ETy->getDecl()->isComplete())
@@ -356,17 +356,34 @@ ArgType::matchesType(ASTContext , QualType argTy) const 
{
 argTy = ETy->getDecl()->getIntegerType();
   }
 
-  if (const BuiltinType *BT = argTy->getAs())
+  if (const auto *BT = argTy->getAs()) {
+// The types are perfectly matched?
 switch (BT->getKind()) {
+default:
+  break;
+case BuiltinType::Char_S:
+case BuiltinType::SChar:
+case BuiltinType::UChar:
+case BuiltinType::Char_U:
+case BuiltinType::Bool:
+  return Match;
+}
+// "Partially matched" because of promotions?
+if (!Ptr) {
+  switch (BT->getKind()) {
   default:
 break;
-  case BuiltinType::Char_S:
-  case BuiltinType::SChar:
-  case BuiltinType::UChar:
-  case BuiltinType::Char_U:
-  case BuiltinType::Bool:
-return Match;
+  case BuiltinType::Int:
+  case BuiltinType::UInt:
+return MatchPromotion;
+  case BuiltinType::Short:
+  case BuiltinType::UShort:
+  case BuiltinType::WChar_S:
+  case BuiltinType::WChar_U:
+return NoMatchPromotionTypeConfusion;
+  }
 }
+  }
   return NoMatch;
 }
 
@@ -383,8 

[clang] ccbc22c - [Sema] fix false -Wcomma being emitted from void returning functions

2022-08-16 Thread YingChi Long via cfe-commits

Author: YingChi Long
Date: 2022-08-16T20:44:38+08:00
New Revision: ccbc22cd8976c285d76e9f66dd5cac2fe908d084

URL: 
https://github.com/llvm/llvm-project/commit/ccbc22cd8976c285d76e9f66dd5cac2fe908d084
DIFF: 
https://github.com/llvm/llvm-project/commit/ccbc22cd8976c285d76e9f66dd5cac2fe908d084.diff

LOG: [Sema] fix false -Wcomma being emitted from void returning functions

Fixes https://github.com/llvm/llvm-project/issues/57151

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D131892

Added: 


Modified: 
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaExpr.cpp
clang/test/SemaCXX/warn-comma-operator.cpp

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ded0b39c27ea5..1da6015ad1610 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -73,7 +73,8 @@ Bug Fixes
   number of arguments cause an assertion fault.
 - Fix multi-level pack expansion of undeclared function parameters.
   This fixes `Issue 56094 
`_.
-
+- Fix `#57151 `_.
+  ``-Wcomma`` is emitted for void returning functions.
 
 Improvements to Clang's diagnostics
 ^^^

diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 03f5ec9848e1f..a2a2e1ed85fc3 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -13957,8 +13957,10 @@ QualType Sema::CheckAssignmentOperands(Expr *LHSExpr, 
ExprResult ,
   return getLangOpts().CPlusPlus ? LHSType : 
LHSType.getAtomicUnqualifiedType();
 }
 
-// Only ignore explicit casts to void.
-static bool IgnoreCommaOperand(const Expr *E) {
+// Scenarios to ignore if expression E is:
+// 1. an explicit cast expression into void
+// 2. a function call expression that returns void
+static bool IgnoreCommaOperand(const Expr *E, const ASTContext ) {
   E = E->IgnoreParens();
 
   if (const CastExpr *CE = dyn_cast(E)) {
@@ -13973,6 +13975,8 @@ static bool IgnoreCommaOperand(const Expr *E) {
 }
   }
 
+  if (const auto *CE = dyn_cast(E))
+return CE->getCallReturnType(Context)->isVoidType();
   return false;
 }
 
@@ -14014,7 +14018,7 @@ void Sema::DiagnoseCommaOperator(const Expr *LHS, 
SourceLocation Loc) {
   }
 
   // Only allow some expressions on LHS to not warn.
-  if (IgnoreCommaOperand(LHS))
+  if (IgnoreCommaOperand(LHS, Context))
 return;
 
   Diag(Loc, diag::warn_comma_operator);

diff  --git a/clang/test/SemaCXX/warn-comma-operator.cpp 
b/clang/test/SemaCXX/warn-comma-operator.cpp
index 635ce01b1c9cf..75148cb416fdb 100644
--- a/clang/test/SemaCXX/warn-comma-operator.cpp
+++ b/clang/test/SemaCXX/warn-comma-operator.cpp
@@ -140,6 +140,27 @@ void test_nested_fixits(void) {
   // CHECK: fix-it:{{.*}}:{[[@LINE-8]]:46-[[@LINE-8]]:46}:")"
 }
 
+
+void void_func();
+int int_func() { return 0; }
+
+void void_function_comma(){
+  void_func(), int_func(); // expected no -Wcomma because of the returning 
type `void` 
+  // Reported by https://github.com/llvm/llvm-project/issues/57151
+  // Descriptions about -Wcomma: https://reviews.llvm.org/D3976
+}
+
+typedef void Void;
+Void typedef_func();
+
+void whatever() {
+  // We don't get confused about type aliases.
+  typedef_func(), int_func();
+  // Even function pointers don't confuse us.
+  void (*fp)() = void_func;
+  fp(), int_func();
+}
+
 #ifdef __cplusplus
 class S2 {
 public:
@@ -296,4 +317,23 @@ void test_dependent_cast() {
   (void)T{}, 0;
   static_cast(T{}), 0;
 }
+
+namespace {
+
+// issue #57151
+
+struct S {
+  void mem() {}
+};
+
+void whatever() {
+  struct S s;
+  // Member function calls also work as expected.
+  s.mem(), int_func();
+  // As do lambda calls.
+  []() { return; }(), int_func();
+}
+
+} // namespace
+
 #endif  // ifdef __cplusplus



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] e582519 - [clang] fix frontend crash when evaluating type trait

2022-08-12 Thread YingChi Long via cfe-commits

Author: YingChi Long
Date: 2022-08-13T00:02:19+08:00
New Revision: e5825190b8ad7ac8fe762fe4101cd4af04f4c057

URL: 
https://github.com/llvm/llvm-project/commit/e5825190b8ad7ac8fe762fe4101cd4af04f4c057
DIFF: 
https://github.com/llvm/llvm-project/commit/e5825190b8ad7ac8fe762fe4101cd4af04f4c057.diff

LOG: [clang] fix frontend crash when evaluating type trait

Before this patch type traits are checked in Parser, so use type traits
directly did not cause assertion faults. However if type traits are initialized
from a template, we didn't perform arity checks before evaluating. This
patch moves arity checks from Parser to Sema, and performing arity
checks in Sema actions, so type traits get checked corretly.

Crash input:

```
template bool b = __is_constructible(Ts...);
bool x = b<>;
```

After this patch:

```
clang/test/SemaCXX/type-trait-eval-crash-issue-57008.cpp:5:32: error: type 
trait requires 1 or more arguments; have 0 arguments
template bool b = __is_constructible(Ts...);
   ^~
clang/test/SemaCXX/type-trait-eval-crash-issue-57008.cpp:6:10: note: in 
instantiation of variable template specialization 'b<>' requested here
bool x = b<>;
 ^
1 error generated.
```

See https://godbolt.org/z/q39W78hsK.

Fixes https://github.com/llvm/llvm-project/issues/57008

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D131423

Added: 
clang/test/SemaCXX/type-trait-eval-crash-issue-57008.cpp

Modified: 
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/TypeTraits.h
clang/include/clang/Sema/Sema.h
clang/lib/Basic/TypeTraits.cpp
clang/lib/Parse/ParseExprCXX.cpp
clang/lib/Sema/SemaExprCXX.cpp

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 572961939d859..6970149444d1e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -68,6 +68,9 @@ Bug Fixes
 - Fixed a crash-on-valid with consteval evaluation of a list-initialized
   constructor for a temporary object. This fixes
   `Issue 55871 `_.
+- Fix `#57008 `_ - Builtin
+  C++ language extension type traits instantiated by a template with unexpected
+  number of arguments cause an assertion fault.
 
 Improvements to Clang's diagnostics
 ^^^

diff  --git a/clang/include/clang/Basic/TypeTraits.h 
b/clang/include/clang/Basic/TypeTraits.h
index a0f06bec6697b..eb8b1923152db 100644
--- a/clang/include/clang/Basic/TypeTraits.h
+++ b/clang/include/clang/Basic/TypeTraits.h
@@ -67,6 +67,10 @@ const char *getTraitName(UnaryExprOrTypeTrait T) 
LLVM_READONLY;
 const char *getTraitSpelling(TypeTrait T) LLVM_READONLY;
 const char *getTraitSpelling(ArrayTypeTrait T) LLVM_READONLY;
 const char *getTraitSpelling(UnaryExprOrTypeTrait T) LLVM_READONLY;
+
+/// Return the arity of the type trait \p T.
+unsigned getTypeTraitArity(TypeTrait T) LLVM_READONLY;
+
 } // namespace clang
 
 #endif

diff  --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index bb2fa139af187..6908eaf8ff954 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5552,6 +5552,8 @@ class Sema final {
   bool isQualifiedMemberAccess(Expr *E);
   QualType CheckAddressOfOperand(ExprResult , SourceLocation OpLoc);
 
+  bool CheckTypeTraitArity(unsigned Arity, SourceLocation Loc, size_t N);
+
   ExprResult CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo,
 SourceLocation OpLoc,
 UnaryExprOrTypeTrait ExprKind,

diff  --git a/clang/lib/Basic/TypeTraits.cpp b/clang/lib/Basic/TypeTraits.cpp
index 3b723afff70b5..4dbf678dc395b 100644
--- a/clang/lib/Basic/TypeTraits.cpp
+++ b/clang/lib/Basic/TypeTraits.cpp
@@ -55,6 +55,15 @@ static constexpr const char *UnaryExprOrTypeTraitSpellings[] 
= {
 #include "clang/Basic/TokenKinds.def"
 };
 
+static constexpr const unsigned TypeTraitArities[] = {
+#define TYPE_TRAIT_1(Spelling, Name, Key) 1,
+#include "clang/Basic/TokenKinds.def"
+#define TYPE_TRAIT_2(Spelling, Name, Key) 2,
+#include "clang/Basic/TokenKinds.def"
+#define TYPE_TRAIT_N(Spelling, Name, Key) 0,
+#include "clang/Basic/TokenKinds.def"
+};
+
 const char *clang::getTraitName(TypeTrait T) {
   assert(T <= TT_Last && "invalid enum value!");
   return TypeTraitNames[T];
@@ -84,3 +93,8 @@ const char *clang::getTraitSpelling(UnaryExprOrTypeTrait T) {
   assert(T <= UETT_Last && "invalid enum value!");
   return UnaryExprOrTypeTraitSpellings[T];
 }
+
+unsigned clang::getTypeTraitArity(TypeTrait T) {
+  assert(T <= TT_Last && "invalid enum value!");
+  return TypeTraitArities[T];
+}

diff  --git a/clang/lib/Parse/ParseExprCXX.cpp 
b/clang/lib/Parse/ParseExprCXX.cpp
index 9bd89eddb4551..2f30075f7c3cd 100644
--- 

[clang] 55d3b79 - [clang] add APValue type check in `TryPrintAsStringLiteral`

2022-08-09 Thread YingChi Long via cfe-commits

Author: YingChi Long
Date: 2022-08-10T08:42:59+08:00
New Revision: 55d3b79d159bab53b140860279486db8a1c0e4f3

URL: 
https://github.com/llvm/llvm-project/commit/55d3b79d159bab53b140860279486db8a1c0e4f3
DIFF: 
https://github.com/llvm/llvm-project/commit/55d3b79d159bab53b140860279486db8a1c0e4f3.diff

LOG: [clang] add APValue type check in `TryPrintAsStringLiteral`

Fixes https://github.com/llvm/llvm-project/issues/57013

https://reviews.llvm.org/D115031 improved printing of non-type template
parameter args. But checking if the end of Inits is 0 without checking
if APValue is an integer, causes clang to segfault. This patch adds
the code to check the type. (May not be a proper bugfix.)

Reviewed By: aaron.ballman, lichray

Differential Revision: https://reviews.llvm.org/D131466

Added: 
clang/test/SemaCXX/try-print-as-string-literal-type-check.cpp

Modified: 
clang/lib/AST/APValue.cpp

Removed: 




diff  --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp
index 05886dc86d17d..634423008c2ed 100644
--- a/clang/lib/AST/APValue.cpp
+++ b/clang/lib/AST/APValue.cpp
@@ -637,10 +637,10 @@ static bool TryPrintAsStringLiteral(raw_ostream ,
 return false;
 
   // Nothing we can do about a sequence that is not null-terminated
-  if (!Inits.back().getInt().isZero())
+  if (!Inits.back().isInt() || !Inits.back().getInt().isZero())
 return false;
-  else
-Inits = Inits.drop_back();
+
+  Inits = Inits.drop_back();
 
   llvm::SmallString<40> Buf;
   Buf.push_back('"');
@@ -655,6 +655,8 @@ static bool TryPrintAsStringLiteral(raw_ostream ,
   }
 
   for (auto  : Inits) {
+if (!Val.isInt())
+  return false;
 int64_t Char64 = Val.getInt().getExtValue();
 if (!isASCII(Char64))
   return false; // Bye bye, see you in integers.

diff  --git a/clang/test/SemaCXX/try-print-as-string-literal-type-check.cpp 
b/clang/test/SemaCXX/try-print-as-string-literal-type-check.cpp
new file mode 100644
index 0..6d1b29e785c31
--- /dev/null
+++ b/clang/test/SemaCXX/try-print-as-string-literal-type-check.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 %s -std=c++20 -fsyntax-only -verify
+// expected-no-diagnostics
+
+// Reported by: https://github.com/llvm/llvm-project/issues/57013
+// The following code should not crash clang
+struct X {
+  char arr[2];
+  constexpr X() {}
+  constexpr void modify() {
+arr[0] = 0;
+  }
+};
+constexpr X f(X t) {
+t.modify();
+return t;
+}
+auto x = f(X());



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 87dc7d4 - [clang][CodeGen] Factor out Swift ABI hooks (NFCI)

2022-08-07 Thread YingChi Long via cfe-commits

Author: Sergei Barannikov
Date: 2022-08-08T00:23:23+08:00
New Revision: 87dc7d4b619d9823b4d5d33bbc3ff3a193b9da2f

URL: 
https://github.com/llvm/llvm-project/commit/87dc7d4b619d9823b4d5d33bbc3ff3a193b9da2f
DIFF: 
https://github.com/llvm/llvm-project/commit/87dc7d4b619d9823b4d5d33bbc3ff3a193b9da2f.diff

LOG: [clang][CodeGen] Factor out Swift ABI hooks (NFCI)

Swift calling conventions stands out in the way that they are lowered in
mostly target-independent manner, with very few customization points.
As such, swift-related methods of ABIInfo do not reference the rest of
ABIInfo and vice versa.
This change follows interface segregation principle; it removes
dependency of SwiftABIInfo on ABIInfo. Targets must now implement
SwiftABIInfo separately if they support Swift calling conventions.

Almost all targets implemented `shouldPassIndirectly` the same way. This
de-facto default implementation has been moved into the base class.

`isSwiftErrorInRegister` used to be virtual, now it is not. It didn't
accept any arguments which could have an effect on the returned value.
This is now a static property of the target ABI.

Reviewed By: rusyaev-roman, inclyc

Differential Revision: https://reviews.llvm.org/D130394

Added: 


Modified: 
clang/lib/CodeGen/ABIInfo.h
clang/lib/CodeGen/SwiftCallingConv.cpp
clang/lib/CodeGen/TargetInfo.cpp
clang/lib/CodeGen/TargetInfo.h

Removed: 




diff  --git a/clang/lib/CodeGen/ABIInfo.h b/clang/lib/CodeGen/ABIInfo.h
index c1eb8a975796..755d2aaa7beb 100644
--- a/clang/lib/CodeGen/ABIInfo.h
+++ b/clang/lib/CodeGen/ABIInfo.h
@@ -33,7 +33,6 @@ namespace CodeGen {
   class CGFunctionInfo;
   class CodeGenFunction;
   class CodeGenTypes;
-  class SwiftABIInfo;
 
   // FIXME: All of this stuff should be part of the target interface
   // somehow. It is currently here because it is not clear how to factor
@@ -44,9 +43,8 @@ namespace CodeGen {
   /// ABIInfo - Target specific hooks for defining how a type should be
   /// passed or returned from functions.
   class ABIInfo {
-  public:
-CodeGen::CodeGenTypes 
   protected:
+CodeGen::CodeGenTypes 
 llvm::CallingConv::ID RuntimeCC;
   public:
 ABIInfo(CodeGen::CodeGenTypes )
@@ -54,8 +52,6 @@ namespace CodeGen {
 
 virtual ~ABIInfo();
 
-virtual bool supportsSwift() const { return false; }
-
 virtual bool allowBFloatArgsAndRet() const { return false; }
 
 CodeGen::CGCXXABI () const;
@@ -114,33 +110,33 @@ namespace CodeGen {
 
 CodeGen::ABIArgInfo
 getNaturalAlignIndirectInReg(QualType Ty, bool Realign = false) const;
-
-
   };
 
-  /// A refining implementation of ABIInfo for targets that support swiftcall.
-  ///
-  /// If we find ourselves wanting multiple such refinements, they'll probably
-  /// be independent refinements, and we should probably find another way
-  /// to do it than simple inheritance.
-  class SwiftABIInfo : public ABIInfo {
-  public:
-SwiftABIInfo(CodeGen::CodeGenTypes ) : ABIInfo(cgt) {}
+  /// Target specific hooks for defining how a type should be passed or 
returned
+  /// from functions with one of the Swift calling conventions.
+  class SwiftABIInfo {
+  protected:
+CodeGenTypes 
+bool SwiftErrorInRegister;
 
-bool supportsSwift() const final { return true; }
+  public:
+SwiftABIInfo(CodeGen::CodeGenTypes , bool SwiftErrorInRegister)
+: CGT(CGT), SwiftErrorInRegister(SwiftErrorInRegister) {}
 
-virtual bool shouldPassIndirectlyForSwift(ArrayRef types,
-  bool asReturnValue) const = 0;
+virtual ~SwiftABIInfo();
 
-virtual bool isLegalVectorTypeForSwift(CharUnits totalSize,
-   llvm::Type *eltTy,
-   unsigned elts) const;
+/// Returns true if an aggregate which expands to the given type sequence
+/// should be passed / returned indirectly.
+virtual bool shouldPassIndirectly(ArrayRef ComponentTys,
+  bool AsReturnValue) const;
 
-virtual bool isSwiftErrorInRegister() const = 0;
+/// Returns true if the given vector type is legal from Swift's calling
+/// convention perspective.
+virtual bool isLegalVectorType(CharUnits VectorSize, llvm::Type *EltTy,
+   unsigned NumElts) const;
 
-static bool classof(const ABIInfo *info) {
-  return info->supportsSwift();
-}
+/// Returns true if swifterror is lowered to a register by the target ABI.
+bool isSwiftErrorInRegister() const { return SwiftErrorInRegister; };
   };
 }  // end namespace CodeGen
 }  // end namespace clang

diff  --git a/clang/lib/CodeGen/SwiftCallingConv.cpp 
b/clang/lib/CodeGen/SwiftCallingConv.cpp
index 8fb24fcecf53..e42859af05a2 100644
--- a/clang/lib/CodeGen/SwiftCallingConv.cpp
+++ b/clang/lib/CodeGen/SwiftCallingConv.cpp
@@ -21,7 +21,7 @@ 

[clang] 1f54006 - [clang][docs] use `Fixes` instead of `This fixes` in ReleaseNotes [NFC]

2022-08-06 Thread YingChi Long via cfe-commits

Author: YingChi Long
Date: 2022-08-07T12:42:15+08:00
New Revision: 1f54006bcacf3967b03f9851edf7f9cd773d53e8

URL: 
https://github.com/llvm/llvm-project/commit/1f54006bcacf3967b03f9851edf7f9cd773d53e8
DIFF: 
https://github.com/llvm/llvm-project/commit/1f54006bcacf3967b03f9851edf7f9cd773d53e8.diff

LOG: [clang][docs] use `Fixes` instead of `This fixes` in ReleaseNotes [NFC]

Added: 


Modified: 
clang/docs/ReleaseNotes.rst

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a2d7526bb770c..4f34b2f946b6e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -74,7 +74,7 @@ Improvements to Clang's diagnostics
   the enumeration's values. Fixes
   `Issue 50055: `_.
 - Clang will now check compile-time determinable string literals as format 
strings.
-  This fixes `Issue 55805: 
`_.
+  Fixes `Issue 55805: `_.
 - ``-Wformat`` now recognizes ``%b`` for the ``printf``/``scanf`` family of
   functions and ``%B`` for the ``printf`` family of functions. Fixes
   `Issue 56885: `_.



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] a6cb841 - clang: fix typo availbility

2022-08-06 Thread YingChi Long via cfe-commits

Author: Aarush Bhat
Date: 2022-08-07T03:44:55+08:00
New Revision: a6cb8419b1757380e3c577477fdc2501484a7245

URL: 
https://github.com/llvm/llvm-project/commit/a6cb8419b1757380e3c577477fdc2501484a7245
DIFF: 
https://github.com/llvm/llvm-project/commit/a6cb8419b1757380e3c577477fdc2501484a7245.diff

LOG: clang: fix typo availbility

- Fixes [[ https://github.com/llvm/llvm-project/issues/56787 | #56787 ]].

I am fixing the spelling of availability.

I am unsure if this change will have any side effects. If someone can
help on how to check if it has any side effects, I can test those out as
well.

Reviewed By: inclyc

Differential Revision: https://reviews.llvm.org/D131277

Added: 


Modified: 
clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp

Removed: 




diff  --git a/clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp 
b/clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp
index 6a88f8d73d4a7..d1bf83c8674f2 100644
--- a/clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp
+++ b/clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp
@@ -494,7 +494,7 @@ SymbolGraphSerializer::serializeAPIRecord(const RecordTy 
) const {
   serializeObject(
   Obj, "location",
   serializeSourceLocation(Record.Location, /*IncludeFileURI=*/true));
-  serializeObject(Obj, "availbility",
+  serializeObject(Obj, "availability",
   serializeAvailability(Record.Availability));
   serializeObject(Obj, "docComment", serializeDocComment(Record.Comment));
   serializeArray(Obj, "declarationFragments",



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 282d475 - [clang] change `auto` to `Expr` in last commit [NFC]

2022-08-04 Thread YingChi Long via cfe-commits

Author: YingChi Long
Date: 2022-08-04T21:15:45+08:00
New Revision: 282d4755c37ced7c2b36526d79033c5c76a006a8

URL: 
https://github.com/llvm/llvm-project/commit/282d4755c37ced7c2b36526d79033c5c76a006a8
DIFF: 
https://github.com/llvm/llvm-project/commit/282d4755c37ced7c2b36526d79033c5c76a006a8.diff

LOG: [clang] change `auto` to `Expr` in last commit [NFC]

Added: 


Modified: 
clang/lib/Sema/SemaChecking.cpp

Removed: 




diff  --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 6428be13f061..d71114e33f31 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8716,7 +8716,7 @@ checkFormatStringExpr(Sema , const Expr *E, 
ArrayRef Args,
 }
   }
 }
-if (const auto *SLE = maybeConstEvalStringLiteral(S.Context, E))
+if (const Expr *SLE = maybeConstEvalStringLiteral(S.Context, E))
   return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg,
Type, CallType, /*InFunctionCall*/ false,
CheckedVarArgs, UncoveredArg, Offset,



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] f417583 - [clang] format string checking for conpile-time evaluated str literal

2022-08-04 Thread YingChi Long via cfe-commits

Author: YingChi Long
Date: 2022-08-04T21:07:30+08:00
New Revision: f417583f319bd60d1e32cdf9d0242e42f86101bf

URL: 
https://github.com/llvm/llvm-project/commit/f417583f319bd60d1e32cdf9d0242e42f86101bf
DIFF: 
https://github.com/llvm/llvm-project/commit/f417583f319bd60d1e32cdf9d0242e42f86101bf.diff

LOG: [clang] format string checking for conpile-time evaluated str literal

This patch enhances clang's ability to check compile-time determinable
string literals as format strings, and can give FixIt hints at literals
(unlike gcc). Issue https://github.com/llvm/llvm-project/issues/55805
mentiond two compile-time string cases. And this patch partially fixes
one.

```
constexpr const char* foo() {
  return "%s %d";
}
int main() {
   printf(foo(), "abc", "def");
   return 0;
}
```

This patch enables clang check format string for this:

```
:4:24: warning: format specifies type 'int' but the argument has type 
'const char *' [-Wformat]
  printf(foo(), "abc", "def");
 ~ ^
:2:42: note: format string is defined here
constexpr const char *foo() { return "%s %d"; }
 ^~
 %s
1 warning generated.
```

Reviewed By: aaron.ballman

Signed-off-by: YingChi Long 

Differential Revision: https://reviews.llvm.org/D130906

Added: 


Modified: 
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaChecking.cpp
clang/test/SemaCXX/format-strings.cpp

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 878effe08294c..85d360eb9cdac 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -67,6 +67,8 @@ Improvements to Clang's diagnostics
   enum without a fixed underlying type is set to a value outside the range of
   the enumeration's values. Fixes
   `Issue 50055: `_.
+- Clang will now check compile-time determinable string literals as format 
strings.
+  This fixes `Issue 55805: 
`_.
 
 Non-comprehensive list of changes in this release
 -

diff  --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 6b6031268b757..6428be13f0610 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8473,6 +8473,9 @@ static void CheckFormatString(
 llvm::SmallBitVector , UncoveredArgHandler ,
 bool IgnoreStringsWithoutSpecifiers);
 
+static const Expr *maybeConstEvalStringLiteral(ASTContext ,
+   const Expr *E);
+
 // Determine if an expression is a string literal or constant string.
 // If this function returns false on the arguments to a function expecting a
 // format string, we will usually need to emit a warning.
@@ -8713,7 +8716,11 @@ checkFormatStringExpr(Sema , const Expr *E, 
ArrayRef Args,
 }
   }
 }
-
+if (const auto *SLE = maybeConstEvalStringLiteral(S.Context, E))
+  return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg,
+   Type, CallType, /*InFunctionCall*/ false,
+   CheckedVarArgs, UncoveredArg, Offset,
+   IgnoreStringsWithoutSpecifiers);
 return SLCT_NotALiteral;
   }
   case Stmt::ObjCMessageExprClass: {
@@ -8823,6 +8830,20 @@ checkFormatStringExpr(Sema , const Expr *E, 
ArrayRef Args,
   }
 }
 
+// If this expression can be evaluated at compile-time,
+// check if the result is a StringLiteral and return it
+// otherwise return nullptr
+static const Expr *maybeConstEvalStringLiteral(ASTContext ,
+   const Expr *E) {
+  Expr::EvalResult Result;
+  if (E->EvaluateAsRValue(Result, Context) && Result.Val.isLValue()) {
+const auto *LVE = Result.Val.getLValueBase().dyn_cast();
+if (isa_and_nonnull(LVE))
+  return LVE;
+  }
+  return nullptr;
+}
+
 Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) {
   return llvm::StringSwitch(Format->getType()->getName())
   .Case("scanf", FST_Scanf)

diff  --git a/clang/test/SemaCXX/format-strings.cpp 
b/clang/test/SemaCXX/format-strings.cpp
index a0dcf01910bb7..2378eea9b3c07 100644
--- a/clang/test/SemaCXX/format-strings.cpp
+++ b/clang/test/SemaCXX/format-strings.cpp
@@ -163,3 +163,48 @@ void f() {
   t::func4("Hello %s"); // expected-warning {{more '%' conversions than data 
arguments}}
 }
 }
+#if __cplusplus >= 201103L
+namespace evaluated {
+
+constexpr const char *basic() { 
+  return 
+"%s %d"; // expected-note {{format string is defined here}}
+}
+
+constexpr const char *correct_fmt() { 
+  return 
+"%d %d";
+}
+
+constexpr const char *string_linebreak() { 
+  return 
+"%d %d"
+"%d %s"; // expected-note {{format string is defined here}}
+}
+
+/*non-constexpr*/