[PATCH] D103313: [RISCV][Clang] Add support for Zmmul extension

2022-07-18 Thread ksyx via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG3198364e6e49: [RISCV][Clang] Add support for Zmmul extension 
(authored by ksyx).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103313/new/

https://reviews.llvm.org/D103313

Files:
  clang/lib/Basic/Targets/RISCV.cpp
  clang/test/Driver/riscv-arch.c
  llvm/lib/Support/RISCVISAInfo.cpp
  llvm/lib/Target/RISCV/RISCV.td
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfoM.td
  llvm/lib/Target/RISCV/RISCVSubtarget.h
  llvm/test/CodeGen/RISCV/attributes.ll
  llvm/test/CodeGen/RISCV/zmmul.ll
  llvm/test/MC/RISCV/rv32i-invalid.s
  llvm/test/MC/RISCV/rv32zmmul-invaild.s
  llvm/test/MC/RISCV/rv32zmmul-valid.s
  llvm/test/MC/RISCV/rv64zmmul-invalid.s
  llvm/test/MC/RISCV/rv64zmmul-valid.s

Index: llvm/test/MC/RISCV/rv64zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-valid.s
@@ -0,0 +1,5 @@
+# RUN: llvm-mc %s -triple=riscv64 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mulw ra, sp, gp
+mulw ra, sp, gp
Index: llvm/test/MC/RISCV/rv64zmmul-invalid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-invalid.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv64 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divw tp, t0, t1
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divuw t2, s0, s2
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remw a0, a1, a2
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remuw a3, a4, a5
Index: llvm/test/MC/RISCV/rv32zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-valid.s
@@ -0,0 +1,14 @@
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mul a4, ra, s0
+mul a4, ra, s0
+
+# CHECK-INST: mulh ra, zero, zero
+mulh x1, x0, x0
+
+# CHECK-INST: mulhsu t0, t2, t1
+mulhsu t0, t2, t1
+
+# CHECK-INST: mulhu a5, a4, a3
+mulhu a5, a4, a3
Index: llvm/test/MC/RISCV/rv32zmmul-invaild.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-invaild.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv32 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div s0, s0, s0
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divu gp, a0, a1
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+rem s2, s2, s8
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remu x18, x18, x24
Index: llvm/test/MC/RISCV/rv32i-invalid.s
===
--- llvm/test/MC/RISCV/rv32i-invalid.s
+++ llvm/test/MC/RISCV/rv32i-invalid.s
@@ -169,7 +169,7 @@
 xor s2, s2 # CHECK: :[[@LINE]]:1: error: too few operands for instruction
 
 # Instruction not in the base ISA
-mul a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
 amomaxu.w s5, s4, (s3) # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'A' (Atomic Instructions)
 fadd.s ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'F' (Single-Precision Floating-Point){{$}}
 fadd.h ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'Zfh' (Half-Precision Floating-Point)
Index: llvm/test/CodeGen/RISCV/zmmul.ll
===
--- /dev/null
+++ llvm/test/CodeGen/RISCV/zmmul.ll
@@ -0,0 +1,41 @@
+; RUN: llc -mtriple=riscv32 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv32 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+; RUN: llc -mtriple=riscv64 

[PATCH] D103313: [RISCV][Clang] Add support for Zmmul extension

2022-07-18 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 445461.
ksyx marked an inline comment as done.
ksyx added a comment.

Update zmmul version & rebase


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103313/new/

https://reviews.llvm.org/D103313

Files:
  clang/lib/Basic/Targets/RISCV.cpp
  clang/test/Driver/riscv-arch.c
  llvm/lib/Support/RISCVISAInfo.cpp
  llvm/lib/Target/RISCV/RISCV.td
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfoM.td
  llvm/lib/Target/RISCV/RISCVSubtarget.h
  llvm/test/CodeGen/RISCV/attributes.ll
  llvm/test/CodeGen/RISCV/zmmul.ll
  llvm/test/MC/RISCV/rv32i-invalid.s
  llvm/test/MC/RISCV/rv32zmmul-invaild.s
  llvm/test/MC/RISCV/rv32zmmul-valid.s
  llvm/test/MC/RISCV/rv64zmmul-invalid.s
  llvm/test/MC/RISCV/rv64zmmul-valid.s

Index: llvm/test/MC/RISCV/rv64zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-valid.s
@@ -0,0 +1,5 @@
+# RUN: llvm-mc %s -triple=riscv64 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mulw ra, sp, gp
+mulw ra, sp, gp
Index: llvm/test/MC/RISCV/rv64zmmul-invalid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-invalid.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv64 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divw tp, t0, t1
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divuw t2, s0, s2
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remw a0, a1, a2
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remuw a3, a4, a5
Index: llvm/test/MC/RISCV/rv32zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-valid.s
@@ -0,0 +1,14 @@
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mul a4, ra, s0
+mul a4, ra, s0
+
+# CHECK-INST: mulh ra, zero, zero
+mulh x1, x0, x0
+
+# CHECK-INST: mulhsu t0, t2, t1
+mulhsu t0, t2, t1
+
+# CHECK-INST: mulhu a5, a4, a3
+mulhu a5, a4, a3
Index: llvm/test/MC/RISCV/rv32zmmul-invaild.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-invaild.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv32 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div s0, s0, s0
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divu gp, a0, a1
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+rem s2, s2, s8
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remu x18, x18, x24
Index: llvm/test/MC/RISCV/rv32i-invalid.s
===
--- llvm/test/MC/RISCV/rv32i-invalid.s
+++ llvm/test/MC/RISCV/rv32i-invalid.s
@@ -169,7 +169,7 @@
 xor s2, s2 # CHECK: :[[@LINE]]:1: error: too few operands for instruction
 
 # Instruction not in the base ISA
-mul a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
 amomaxu.w s5, s4, (s3) # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'A' (Atomic Instructions)
 fadd.s ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'F' (Single-Precision Floating-Point){{$}}
 fadd.h ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'Zfh' (Half-Precision Floating-Point)
Index: llvm/test/CodeGen/RISCV/zmmul.ll
===
--- /dev/null
+++ llvm/test/CodeGen/RISCV/zmmul.ll
@@ -0,0 +1,41 @@
+; RUN: llc -mtriple=riscv32 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv32 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+; RUN: llc -mtriple=riscv64 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+
+; RUN: llc -mtriple=riscv32 

[PATCH] D103313: [RISCV][Clang] Add support for Zmmul extension

2022-07-17 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 445332.
ksyx added a comment.

Rebase and update use of M extension existence check to include Zmmul whenever 
needed.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103313/new/

https://reviews.llvm.org/D103313

Files:
  clang/lib/Basic/Targets/RISCV.cpp
  clang/test/Driver/riscv-arch.c
  llvm/lib/Support/RISCVISAInfo.cpp
  llvm/lib/Target/RISCV/RISCV.td
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfoM.td
  llvm/lib/Target/RISCV/RISCVSubtarget.h
  llvm/test/CodeGen/RISCV/attributes.ll
  llvm/test/CodeGen/RISCV/zmmul.ll
  llvm/test/MC/RISCV/rv32i-invalid.s
  llvm/test/MC/RISCV/rv32zmmul-invaild.s
  llvm/test/MC/RISCV/rv32zmmul-valid.s
  llvm/test/MC/RISCV/rv64zmmul-invalid.s
  llvm/test/MC/RISCV/rv64zmmul-valid.s

Index: llvm/test/MC/RISCV/rv64zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-valid.s
@@ -0,0 +1,5 @@
+# RUN: llvm-mc %s -triple=riscv64 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mulw ra, sp, gp
+mulw ra, sp, gp
Index: llvm/test/MC/RISCV/rv64zmmul-invalid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-invalid.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv64 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divw tp, t0, t1
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divuw t2, s0, s2
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remw a0, a1, a2
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remuw a3, a4, a5
Index: llvm/test/MC/RISCV/rv32zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-valid.s
@@ -0,0 +1,14 @@
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mul a4, ra, s0
+mul a4, ra, s0
+
+# CHECK-INST: mulh ra, zero, zero
+mulh x1, x0, x0
+
+# CHECK-INST: mulhsu t0, t2, t1
+mulhsu t0, t2, t1
+
+# CHECK-INST: mulhu a5, a4, a3
+mulhu a5, a4, a3
Index: llvm/test/MC/RISCV/rv32zmmul-invaild.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-invaild.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv32 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div s0, s0, s0
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divu gp, a0, a1
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+rem s2, s2, s8
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remu x18, x18, x24
Index: llvm/test/MC/RISCV/rv32i-invalid.s
===
--- llvm/test/MC/RISCV/rv32i-invalid.s
+++ llvm/test/MC/RISCV/rv32i-invalid.s
@@ -169,7 +169,7 @@
 xor s2, s2 # CHECK: :[[@LINE]]:1: error: too few operands for instruction
 
 # Instruction not in the base ISA
-mul a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
 amomaxu.w s5, s4, (s3) # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'A' (Atomic Instructions)
 fadd.s ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'F' (Single-Precision Floating-Point){{$}}
 fadd.h ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'Zfh' (Half-Precision Floating-Point)
Index: llvm/test/CodeGen/RISCV/zmmul.ll
===
--- /dev/null
+++ llvm/test/CodeGen/RISCV/zmmul.ll
@@ -0,0 +1,41 @@
+; RUN: llc -mtriple=riscv32 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv32 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+; RUN: llc -mtriple=riscv64 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+
+; RUN: llc 

[PATCH] D103313: [RISCV][Clang] Add support for Zmmul extension

2022-07-04 Thread ksyx via Phabricator via cfe-commits
ksyx added a comment.

ping :)


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103313/new/

https://reviews.llvm.org/D103313

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


[PATCH] D103313: [RISCV][Clang] Add support for Zmmul extension

2022-06-26 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 440075.
ksyx marked an inline comment as done.
ksyx added a comment.

Make Zmmul extension independent instead of implied by M extension for backward 
ELF attribute compatibility.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103313/new/

https://reviews.llvm.org/D103313

Files:
  clang/lib/Basic/Targets/RISCV.cpp
  clang/test/Driver/riscv-arch.c
  llvm/lib/Support/RISCVISAInfo.cpp
  llvm/lib/Target/RISCV/RISCV.td
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfoM.td
  llvm/lib/Target/RISCV/RISCVSubtarget.h
  llvm/test/CodeGen/RISCV/attributes.ll
  llvm/test/CodeGen/RISCV/zmmul.ll
  llvm/test/MC/RISCV/rv32i-invalid.s
  llvm/test/MC/RISCV/rv32zmmul-invaild.s
  llvm/test/MC/RISCV/rv32zmmul-valid.s
  llvm/test/MC/RISCV/rv64zmmul-invalid.s
  llvm/test/MC/RISCV/rv64zmmul-valid.s

Index: llvm/test/MC/RISCV/rv64zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-valid.s
@@ -0,0 +1,5 @@
+# RUN: llvm-mc %s -triple=riscv64 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mulw ra, sp, gp
+mulw ra, sp, gp
Index: llvm/test/MC/RISCV/rv64zmmul-invalid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-invalid.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv64 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divw tp, t0, t1
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divuw t2, s0, s2
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remw a0, a1, a2
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remuw a3, a4, a5
Index: llvm/test/MC/RISCV/rv32zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-valid.s
@@ -0,0 +1,14 @@
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mul a4, ra, s0
+mul a4, ra, s0
+
+# CHECK-INST: mulh ra, zero, zero
+mulh x1, x0, x0
+
+# CHECK-INST: mulhsu t0, t2, t1
+mulhsu t0, t2, t1
+
+# CHECK-INST: mulhu a5, a4, a3
+mulhu a5, a4, a3
Index: llvm/test/MC/RISCV/rv32zmmul-invaild.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-invaild.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv32 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div s0, s0, s0
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divu gp, a0, a1
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+rem s2, s2, s8
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remu x18, x18, x24
Index: llvm/test/MC/RISCV/rv32i-invalid.s
===
--- llvm/test/MC/RISCV/rv32i-invalid.s
+++ llvm/test/MC/RISCV/rv32i-invalid.s
@@ -169,7 +169,7 @@
 xor s2, s2 # CHECK: :[[@LINE]]:1: error: too few operands for instruction
 
 # Instruction not in the base ISA
-mul a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
 amomaxu.w s5, s4, (s3) # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'A' (Atomic Instructions)
 fadd.s ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'F' (Single-Precision Floating-Point){{$}}
 fadd.h ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'Zfh' (Half-Precision Floating-Point)
Index: llvm/test/CodeGen/RISCV/zmmul.ll
===
--- /dev/null
+++ llvm/test/CodeGen/RISCV/zmmul.ll
@@ -0,0 +1,41 @@
+; RUN: llc -mtriple=riscv32 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv32 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+; RUN: llc -mtriple=riscv64 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+

[PATCH] D103313: [RISCV][Clang] Add support for Zmmul extension

2022-06-25 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 440017.
ksyx retitled this revision from "[RISCV][Clang] Implement support for 
zmmul-experimental" to "[RISCV][Clang] Add support for Zmmul extension".
ksyx edited the summary of this revision.
ksyx added a comment.
Herald added subscribers: sunshaoce, StephenFan, arichardson.

The Zmmul extension has just ratified 
.
 Update patch to rebase to current main.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103313/new/

https://reviews.llvm.org/D103313

Files:
  clang/lib/Basic/Targets/RISCV.cpp
  clang/test/Driver/riscv-arch.c
  llvm/lib/Support/RISCVISAInfo.cpp
  llvm/lib/Target/RISCV/RISCV.td
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfoM.td
  llvm/lib/Target/RISCV/RISCVSubtarget.h
  llvm/test/CodeGen/RISCV/attributes.ll
  llvm/test/CodeGen/RISCV/zmmul.ll
  llvm/test/MC/RISCV/rv32i-invalid.s
  llvm/test/MC/RISCV/rv32zmmul-invaild.s
  llvm/test/MC/RISCV/rv32zmmul-valid.s
  llvm/test/MC/RISCV/rv64zmmul-invalid.s
  llvm/test/MC/RISCV/rv64zmmul-valid.s

Index: llvm/test/MC/RISCV/rv64zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-valid.s
@@ -0,0 +1,5 @@
+# RUN: llvm-mc %s -triple=riscv64 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mulw ra, sp, gp
+mulw ra, sp, gp
Index: llvm/test/MC/RISCV/rv64zmmul-invalid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-invalid.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv64 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divw tp, t0, t1
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divuw t2, s0, s2
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remw a0, a1, a2
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remuw a3, a4, a5
Index: llvm/test/MC/RISCV/rv32zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-valid.s
@@ -0,0 +1,14 @@
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mul a4, ra, s0
+mul a4, ra, s0
+
+# CHECK-INST: mulh ra, zero, zero
+mulh x1, x0, x0
+
+# CHECK-INST: mulhsu t0, t2, t1
+mulhsu t0, t2, t1
+
+# CHECK-INST: mulhu a5, a4, a3
+mulhu a5, a4, a3
Index: llvm/test/MC/RISCV/rv32zmmul-invaild.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-invaild.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv32 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div s0, s0, s0
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divu gp, a0, a1
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+rem s2, s2, s8
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remu x18, x18, x24
Index: llvm/test/MC/RISCV/rv32i-invalid.s
===
--- llvm/test/MC/RISCV/rv32i-invalid.s
+++ llvm/test/MC/RISCV/rv32i-invalid.s
@@ -169,7 +169,7 @@
 xor s2, s2 # CHECK: :[[@LINE]]:1: error: too few operands for instruction
 
 # Instruction not in the base ISA
-mul a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
 amomaxu.w s5, s4, (s3) # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'A' (Atomic Instructions)
 fadd.s ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'F' (Single-Precision Floating-Point){{$}}
 fadd.h ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'Zfh' (Half-Precision Floating-Point)
Index: llvm/test/CodeGen/RISCV/zmmul.ll
===
--- /dev/null
+++ llvm/test/CodeGen/RISCV/zmmul.ll
@@ -0,0 +1,41 @@
+; RUN: llc -mtriple=riscv32 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+zmmul 

[PATCH] D123676: [clang-format] Fix WhitespaceSensitiveMacros not being honoured when macro closing parenthesis is followed by a newline.

2022-05-06 Thread ksyx via Phabricator via cfe-commits
ksyx accepted this revision.
ksyx added a comment.

Thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D123676/new/

https://reviews.llvm.org/D123676

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


[PATCH] D123676: [clang-format] Fix WhitespaceSensitiveMacros not being honoured when macro closing parenthesis is followed by a newline.

2022-04-13 Thread ksyx via Phabricator via cfe-commits
ksyx added inline comments.



Comment at: clang/lib/Format/FormatTokenLexer.h:117-120
+  struct MacroTokenInfo {
+TokenType Type;
+bool Finalized;
+  };

curdeius wrote:
> ksyx wrote:
> > Would making constructor of `struct MacroTokenInfo` having default 
> > parameter or overloading it help avoiding the change of adding `, 
> > /*Finalized=*/false` to the existing initializer lists?
> I've thought about it, but it would mean that we have a non-explicit 1-arg 
> ctor. I'm not a big fan of these as they trigger implicit conversions.
> I can do though:
> ```
>   struct MacroTokenInfo {
> TokenType Type;
> bool Finalized{false};
>   };
> ```
> but we'd still need adding braces in:
> ```
> Macros.insert({Identifier, {TT_ForEachMacro}});
> ```
Yes they are both good point to consider and my start point is just that the 
`finalized` property is less frequently be `true`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D123676/new/

https://reviews.llvm.org/D123676

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


[PATCH] D123676: [clang-format] Fix WhitespaceSensitiveMacros not being honoured when macro closing parenthesis is followed by a newline.

2022-04-13 Thread ksyx via Phabricator via cfe-commits
ksyx added inline comments.



Comment at: clang/lib/Format/FormatTokenLexer.h:117-120
+  struct MacroTokenInfo {
+TokenType Type;
+bool Finalized;
+  };

Would making constructor of `struct MacroTokenInfo` having default parameter or 
overloading it help avoiding the change of adding `, /*Finalized=*/false` to 
the existing initializer lists?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D123676/new/

https://reviews.llvm.org/D123676

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


[PATCH] D103313: [RISCV][Clang] Implement support for zmmul-experimental

2022-03-05 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 413212.
ksyx added a comment.

Fix patch apply failed


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103313/new/

https://reviews.llvm.org/D103313

Files:
  clang/lib/Basic/Targets/RISCV.cpp
  clang/test/Driver/riscv-arch.c
  llvm/lib/Support/RISCVISAInfo.cpp
  llvm/lib/Target/RISCV/RISCV.td
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfoM.td
  llvm/lib/Target/RISCV/RISCVSubtarget.h
  llvm/test/CodeGen/RISCV/attributes.ll
  llvm/test/CodeGen/RISCV/zmmul.ll
  llvm/test/MC/RISCV/rv32i-invalid.s
  llvm/test/MC/RISCV/rv32zmmul-invaild.s
  llvm/test/MC/RISCV/rv32zmmul-valid.s
  llvm/test/MC/RISCV/rv64zmmul-invalid.s
  llvm/test/MC/RISCV/rv64zmmul-valid.s

Index: llvm/test/MC/RISCV/rv64zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-valid.s
@@ -0,0 +1,5 @@
+# RUN: llvm-mc %s -triple=riscv64 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mulw ra, sp, gp
+mulw ra, sp, gp
Index: llvm/test/MC/RISCV/rv64zmmul-invalid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-invalid.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv64 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divw tp, t0, t1
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divuw t2, s0, s2
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remw a0, a1, a2
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remuw a3, a4, a5
Index: llvm/test/MC/RISCV/rv32zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-valid.s
@@ -0,0 +1,14 @@
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mul a4, ra, s0
+mul a4, ra, s0
+
+# CHECK-INST: mulh ra, zero, zero
+mulh x1, x0, x0
+
+# CHECK-INST: mulhsu t0, t2, t1
+mulhsu t0, t2, t1
+
+# CHECK-INST: mulhu a5, a4, a3
+mulhu a5, a4, a3
Index: llvm/test/MC/RISCV/rv32zmmul-invaild.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-invaild.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv32 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div s0, s0, s0
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divu gp, a0, a1
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+rem s2, s2, s8
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remu x18, x18, x24
Index: llvm/test/MC/RISCV/rv32i-invalid.s
===
--- llvm/test/MC/RISCV/rv32i-invalid.s
+++ llvm/test/MC/RISCV/rv32i-invalid.s
@@ -169,7 +169,7 @@
 xor s2, s2 # CHECK: :[[@LINE]]:1: error: too few operands for instruction
 
 # Instruction not in the base ISA
-mul a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
 amomaxu.w s5, s4, (s3) # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'A' (Atomic Instructions)
 fadd.s ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'F' (Single-Precision Floating-Point){{$}}
 fadd.h ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'Zfh' (Half-Precision Floating-Point)
Index: llvm/test/CodeGen/RISCV/zmmul.ll
===
--- /dev/null
+++ llvm/test/CodeGen/RISCV/zmmul.ll
@@ -0,0 +1,41 @@
+; RUN: llc -mtriple=riscv32 -mattr=+experimental-zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+experimental-zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv32 -mattr=+experimental-zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+; RUN: llc -mtriple=riscv64 -mattr=+experimental-zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+
+; RUN: llc 

[PATCH] D103313: [RISCV][Clang] Implement support for zmmul-experimental

2022-03-05 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 413211.
ksyx added a comment.
Herald added a subscriber: jdoerfert.

Rebase and adapt to new ISAInfo module.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103313/new/

https://reviews.llvm.org/D103313

Files:
  clang/lib/Basic/Targets/RISCV.cpp
  clang/test/Driver/riscv-arch.c
  llvm/lib/Support/RISCVISAInfo.cpp
  llvm/lib/Target/RISCV/RISCV.td
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfoM.td
  llvm/lib/Target/RISCV/RISCVSubtarget.h
  llvm/test/CodeGen/RISCV/attributes.ll
  llvm/test/CodeGen/RISCV/zmmul.ll
  llvm/test/MC/RISCV/rv32i-invalid.s
  llvm/test/MC/RISCV/rv32zmmul-invaild.s
  llvm/test/MC/RISCV/rv32zmmul-valid.s
  llvm/test/MC/RISCV/rv64zmmul-invalid.s
  llvm/test/MC/RISCV/rv64zmmul-valid.s

Index: llvm/test/MC/RISCV/rv64zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-valid.s
@@ -0,0 +1,5 @@
+# RUN: llvm-mc %s -triple=riscv64 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mulw ra, sp, gp
+mulw ra, sp, gp
Index: llvm/test/MC/RISCV/rv64zmmul-invalid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-invalid.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv64 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divw tp, t0, t1
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divuw t2, s0, s2
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remw a0, a1, a2
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remuw a3, a4, a5
Index: llvm/test/MC/RISCV/rv32zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-valid.s
@@ -0,0 +1,14 @@
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mul a4, ra, s0
+mul a4, ra, s0
+
+# CHECK-INST: mulh ra, zero, zero
+mulh x1, x0, x0
+
+# CHECK-INST: mulhsu t0, t2, t1
+mulhsu t0, t2, t1
+
+# CHECK-INST: mulhu a5, a4, a3
+mulhu a5, a4, a3
Index: llvm/test/MC/RISCV/rv32zmmul-invaild.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-invaild.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv32 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div s0, s0, s0
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divu gp, a0, a1
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+rem s2, s2, s8
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remu x18, x18, x24
Index: llvm/test/MC/RISCV/rv32i-invalid.s
===
--- llvm/test/MC/RISCV/rv32i-invalid.s
+++ llvm/test/MC/RISCV/rv32i-invalid.s
@@ -169,7 +169,7 @@
 xor s2, s2 # CHECK: :[[@LINE]]:1: error: too few operands for instruction
 
 # Instruction not in the base ISA
-mul a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
 amomaxu.w s5, s4, (s3) # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'A' (Atomic Instructions)
 fadd.s ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'F' (Single-Precision Floating-Point){{$}}
 fadd.h ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'Zfh' (Half-Precision Floating-Point)
Index: llvm/test/CodeGen/RISCV/zmmul.ll
===
--- /dev/null
+++ llvm/test/CodeGen/RISCV/zmmul.ll
@@ -0,0 +1,41 @@
+; RUN: llc -mtriple=riscv32 -mattr=+experimental-zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+experimental-zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv32 -mattr=+experimental-zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+; RUN: llc -mtriple=riscv64 -mattr=+experimental-zmmul -verify-machineinstrs < %s \
+; RUN:  | not 

[PATCH] D118869: [clang-format] Non-latin comment prefix whitespace

2022-03-04 Thread ksyx via Phabricator via cfe-commits
ksyx added a comment.

In D118869#3359439 , @krasimir wrote:

> It appears that this caused a regression by adding an additional space of 
> indentation to line comments in some cases:
>
>   % cat test.cc  # (before)
>   // Comment
>   int i;
>   % clang-format -style=google test.cc
>   //  Comment
>   int i;
>
> @ksyx could you please take a look?

I did not reproduce it in latest main branch. This problem may in relate to 
this issue which have recently been fixed: 
https://github.com/llvm/llvm-project/issues/53844


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118869/new/

https://reviews.llvm.org/D118869

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


[PATCH] D103313: [RISCV][Clang] Implement support for zmmul-experimental

2022-03-04 Thread ksyx via Phabricator via cfe-commits
ksyx added a comment.

In D103313#3359310 , @JiahaoChenConor 
wrote:

> Hi, do you still continue working on this patch?

In fact, not pretty sure what else I need to improve on this.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103313/new/

https://reviews.llvm.org/D103313

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


[PATCH] D119067: [clang-format] Fix DefinitionBlockSeparator extra empty lines

2022-02-07 Thread ksyx via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGa70549ae43df: [clang-format] Fix DefSeparator empty line 
issues (authored by ksyx).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D119067/new/

https://reviews.llvm.org/D119067

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -109,6 +109,15 @@
"};",
Style);
 
+  verifyFormat("union foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "union bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
   verifyFormat("class foo {\n"
"  int i, j;\n"
"};\n"
@@ -311,6 +320,9 @@
   "int bar3(int j, int k, const enum Bar b) {\n"
   "  // A comment\n"
   "  int r = j % k;\n"
+  "  if (struct S = getS()) {\n"
+  "// if condition\n"
+  "  }\n"
   "  return r;\n"
   "}\n";
   std::string Postfix = "\n"
@@ -364,6 +376,9 @@
 "int bar3(int j, int k, const enum Bar b) {\n"
 "  // A comment\n"
 "  int r = j % k;\n"
+"  if (struct S = getS()) {\n"
+"// if condition\n"
+"  }\n"
 "  return r;\n"
 "}\n"
 "} // namespace";
@@ -425,6 +440,10 @@
"{\n"
"  // A comment\n"
"  int r = j % k;\n"
+   "  if (struct S = getS())\n"
+   "  {\n"
+   "// if condition\n"
+   "  }\n"
"  return r;\n"
"}\n"
"} // namespace NS",
@@ -473,6 +492,9 @@
 "int bar3(int j, int k, const enum Bar b) {\n"
 "  // A comment\n"
 "  int r = j % k;\n"
+"  if (struct S = getS()) {\n"
+"// if condition\n"
+"  }\n"
 "  return r;\n"
 "}\n"
 "} // namespace";
Index: clang/lib/Format/DefinitionBlockSeparator.cpp
===
--- clang/lib/Format/DefinitionBlockSeparator.cpp
+++ clang/lib/Format/DefinitionBlockSeparator.cpp
@@ -35,19 +35,31 @@
   const bool IsNeverStyle =
   Style.SeparateDefinitionBlocks == FormatStyle::SDS_Never;
   const AdditionalKeywords  = Tokens.getKeywords();
-  auto LikelyDefinition = [this, ExtraKeywords](const AnnotatedLine *Line,
-bool ExcludeEnum = false) {
+  auto GetBracketLevelChange = [](const FormatToken *Tok) {
+if (Tok->isOneOf(tok::l_brace, tok::l_paren, tok::l_square))
+  return 1;
+if (Tok->isOneOf(tok::r_brace, tok::r_paren, tok::r_square))
+  return -1;
+return 0;
+  };
+  auto LikelyDefinition = [&](const AnnotatedLine *Line,
+  bool ExcludeEnum = false) {
 if ((Line->MightBeFunctionDecl && Line->mightBeFunctionDefinition()) ||
 Line->startsWithNamespace())
   return true;
-FormatToken *CurrentToken = Line->First;
-while (CurrentToken) {
-  if (CurrentToken->isOneOf(tok::kw_class, tok::kw_struct) ||
-  (Style.isJavaScript() && CurrentToken->is(ExtraKeywords.kw_function)))
-return true;
-  if (!ExcludeEnum && CurrentToken->is(tok::kw_enum))
-return true;
-  CurrentToken = CurrentToken->Next;
+int BracketLevel = 0;
+for (const FormatToken *CurrentToken = Line->First; CurrentToken;
+ CurrentToken = CurrentToken->Next) {
+  if (BracketLevel == 0) {
+if ((CurrentToken->isOneOf(tok::kw_class, tok::kw_struct,
+   tok::kw_union) ||
+ (Style.isJavaScript() &&
+  CurrentToken->is(ExtraKeywords.kw_function
+  return true;
+if (!ExcludeEnum && CurrentToken->is(tok::kw_enum))
+  return true;
+  }
+  BracketLevel += GetBracketLevelChange(CurrentToken);
 }
 return false;
   };
@@ -102,14 +114,17 @@
  IsPPConditional(OpeningLineIndex - 1);
 };
 const auto HasEnumOnLine = [&]() {
-  FormatToken *CurrentToken = CurrentLine->First;
   bool FoundEnumKeyword = false;
-  while (CurrentToken) {
-if (CurrentToken->is(tok::kw_enum))
-  

[PATCH] D119067: [clang-format] Fix DefinitionBlockSeparator extra empty lines

2022-02-07 Thread ksyx via Phabricator via cfe-commits
ksyx marked an inline comment as done.
ksyx added inline comments.



Comment at: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp:305
   "  int r = j / k;\n"
+  "  if (struct S = getS()) {\n"
+  "// if condition\n"

curdeius wrote:
> curdeius wrote:
> > I'd rather see this added in another function e.g. bar3, because otherwise 
> > you don't test what was tested before.
> When I wrote this comment I thought that bar3 didn't exist... But well, I 
> think I'll create a patch to clean up these tests a bit anyway.
Thank you very much for this!


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D119067/new/

https://reviews.llvm.org/D119067

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


[PATCH] D119067: [clang-format] Fix DefinitionBlockSeparator extra empty lines

2022-02-06 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 406309.
ksyx added a comment.

Use for loop to go through tokens in a line.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D119067/new/

https://reviews.llvm.org/D119067

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -109,6 +109,15 @@
"};",
Style);
 
+  verifyFormat("union foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "union bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
   verifyFormat("class foo {\n"
"  int i, j;\n"
"};\n"
@@ -311,6 +320,9 @@
   "int bar3(int j, int k, const enum Bar b) {\n"
   "  // A comment\n"
   "  int r = j % k;\n"
+  "  if (struct S = getS()) {\n"
+  "// if condition\n"
+  "  }\n"
   "  return r;\n"
   "}\n";
   std::string Postfix = "\n"
@@ -364,6 +376,9 @@
 "int bar3(int j, int k, const enum Bar b) {\n"
 "  // A comment\n"
 "  int r = j % k;\n"
+"  if (struct S = getS()) {\n"
+"// if condition\n"
+"  }\n"
 "  return r;\n"
 "}\n"
 "} // namespace";
@@ -425,6 +440,10 @@
"{\n"
"  // A comment\n"
"  int r = j % k;\n"
+   "  if (struct S = getS())\n"
+   "  {\n"
+   "// if condition\n"
+   "  }\n"
"  return r;\n"
"}\n"
"} // namespace NS",
@@ -473,6 +492,9 @@
 "int bar3(int j, int k, const enum Bar b) {\n"
 "  // A comment\n"
 "  int r = j % k;\n"
+"  if (struct S = getS()) {\n"
+"// if condition\n"
+"  }\n"
 "  return r;\n"
 "}\n"
 "} // namespace";
Index: clang/lib/Format/DefinitionBlockSeparator.cpp
===
--- clang/lib/Format/DefinitionBlockSeparator.cpp
+++ clang/lib/Format/DefinitionBlockSeparator.cpp
@@ -35,19 +35,31 @@
   const bool IsNeverStyle =
   Style.SeparateDefinitionBlocks == FormatStyle::SDS_Never;
   const AdditionalKeywords  = Tokens.getKeywords();
-  auto LikelyDefinition = [this, ExtraKeywords](const AnnotatedLine *Line,
-bool ExcludeEnum = false) {
+  auto GetBracketLevelChange = [](const FormatToken *Tok) {
+if (Tok->isOneOf(tok::l_brace, tok::l_paren, tok::l_square))
+  return 1;
+if (Tok->isOneOf(tok::r_brace, tok::r_paren, tok::r_square))
+  return -1;
+return 0;
+  };
+  auto LikelyDefinition = [&](const AnnotatedLine *Line,
+  bool ExcludeEnum = false) {
 if ((Line->MightBeFunctionDecl && Line->mightBeFunctionDefinition()) ||
 Line->startsWithNamespace())
   return true;
-FormatToken *CurrentToken = Line->First;
-while (CurrentToken) {
-  if (CurrentToken->isOneOf(tok::kw_class, tok::kw_struct) ||
-  (Style.isJavaScript() && CurrentToken->is(ExtraKeywords.kw_function)))
-return true;
-  if (!ExcludeEnum && CurrentToken->is(tok::kw_enum))
-return true;
-  CurrentToken = CurrentToken->Next;
+int BracketLevel = 0;
+for (const FormatToken *CurrentToken = Line->First; CurrentToken;
+ CurrentToken = CurrentToken->Next) {
+  if (BracketLevel == 0) {
+if ((CurrentToken->isOneOf(tok::kw_class, tok::kw_struct,
+   tok::kw_union) ||
+ (Style.isJavaScript() &&
+  CurrentToken->is(ExtraKeywords.kw_function
+  return true;
+if (!ExcludeEnum && CurrentToken->is(tok::kw_enum))
+  return true;
+  }
+  BracketLevel += GetBracketLevelChange(CurrentToken);
 }
 return false;
   };
@@ -102,14 +114,17 @@
  IsPPConditional(OpeningLineIndex - 1);
 };
 const auto HasEnumOnLine = [&]() {
-  FormatToken *CurrentToken = CurrentLine->First;
   bool FoundEnumKeyword = false;
-  while (CurrentToken) {
-if (CurrentToken->is(tok::kw_enum))
-  FoundEnumKeyword = true;
-else if (FoundEnumKeyword && CurrentToken->is(tok::l_brace))
-  

[PATCH] D119067: [clang-format] Fix DefinitionBlockSeparator extra empty lines

2022-02-06 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 406271.
ksyx added a comment.

- Remove return after else


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D119067/new/

https://reviews.llvm.org/D119067

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -109,6 +109,15 @@
"};",
Style);
 
+  verifyFormat("union foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "union bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
   verifyFormat("class foo {\n"
"  int i, j;\n"
"};\n"
@@ -311,6 +320,9 @@
   "int bar3(int j, int k, const enum Bar b) {\n"
   "  // A comment\n"
   "  int r = j % k;\n"
+  "  if (struct S = getS()) {\n"
+  "// if condition\n"
+  "  }\n"
   "  return r;\n"
   "}\n";
   std::string Postfix = "\n"
@@ -364,6 +376,9 @@
 "int bar3(int j, int k, const enum Bar b) {\n"
 "  // A comment\n"
 "  int r = j % k;\n"
+"  if (struct S = getS()) {\n"
+"// if condition\n"
+"  }\n"
 "  return r;\n"
 "}\n"
 "} // namespace";
@@ -425,6 +440,10 @@
"{\n"
"  // A comment\n"
"  int r = j % k;\n"
+   "  if (struct S = getS())\n"
+   "  {\n"
+   "// if condition\n"
+   "  }\n"
"  return r;\n"
"}\n"
"} // namespace NS",
@@ -473,6 +492,9 @@
 "int bar3(int j, int k, const enum Bar b) {\n"
 "  // A comment\n"
 "  int r = j % k;\n"
+"  if (struct S = getS()) {\n"
+"// if condition\n"
+"  }\n"
 "  return r;\n"
 "}\n"
 "} // namespace";
Index: clang/lib/Format/DefinitionBlockSeparator.cpp
===
--- clang/lib/Format/DefinitionBlockSeparator.cpp
+++ clang/lib/Format/DefinitionBlockSeparator.cpp
@@ -35,18 +35,31 @@
   const bool IsNeverStyle =
   Style.SeparateDefinitionBlocks == FormatStyle::SDS_Never;
   const AdditionalKeywords  = Tokens.getKeywords();
-  auto LikelyDefinition = [this, ExtraKeywords](const AnnotatedLine *Line,
-bool ExcludeEnum = false) {
+  auto GetBracketLevelChange = [](const FormatToken *Tok) {
+if (Tok->isOneOf(tok::l_brace, tok::l_paren, tok::l_square))
+  return 1;
+if (Tok->isOneOf(tok::r_brace, tok::r_paren, tok::r_square))
+  return -1;
+return 0;
+  };
+  auto LikelyDefinition = [&](const AnnotatedLine *Line,
+  bool ExcludeEnum = false) {
 if ((Line->MightBeFunctionDecl && Line->mightBeFunctionDefinition()) ||
 Line->startsWithNamespace())
   return true;
 FormatToken *CurrentToken = Line->First;
+int BracketLevel = 0;
 while (CurrentToken) {
-  if (CurrentToken->isOneOf(tok::kw_class, tok::kw_struct) ||
-  (Style.isJavaScript() && CurrentToken->is(ExtraKeywords.kw_function)))
-return true;
-  if (!ExcludeEnum && CurrentToken->is(tok::kw_enum))
-return true;
+  if (BracketLevel == 0) {
+if ((CurrentToken->isOneOf(tok::kw_class, tok::kw_struct,
+   tok::kw_union) ||
+ (Style.isJavaScript() &&
+  CurrentToken->is(ExtraKeywords.kw_function
+  return true;
+if (!ExcludeEnum && CurrentToken->is(tok::kw_enum))
+  return true;
+  }
+  BracketLevel += GetBracketLevelChange(CurrentToken);
   CurrentToken = CurrentToken->Next;
 }
 return false;
@@ -104,11 +117,15 @@
 const auto HasEnumOnLine = [&]() {
   FormatToken *CurrentToken = CurrentLine->First;
   bool FoundEnumKeyword = false;
+  int BracketLevel = 0;
   while (CurrentToken) {
-if (CurrentToken->is(tok::kw_enum))
-  FoundEnumKeyword = true;
-else if (FoundEnumKeyword && CurrentToken->is(tok::l_brace))
-  return true;
+if (BracketLevel == 0) {
+  if (CurrentToken->is(tok::kw_enum))
+FoundEnumKeyword = true;
+  else if (FoundEnumKeyword && 

[PATCH] D119067: [clang-format] Fix DefinitionBlockSeparator extra empty lines

2022-02-06 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 406245.
ksyx marked 3 inline comments as done.
ksyx edited the summary of this revision.
ksyx added a comment.

- Add or remove empty lines surrounding `union`s.
- Move new test location.
- Remove else immediately above return.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D119067/new/

https://reviews.llvm.org/D119067

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -109,6 +109,15 @@
"};",
Style);
 
+  verifyFormat("union foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "union bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
   verifyFormat("class foo {\n"
"  int i, j;\n"
"};\n"
@@ -311,6 +320,9 @@
   "int bar3(int j, int k, const enum Bar b) {\n"
   "  // A comment\n"
   "  int r = j % k;\n"
+  "  if (struct S = getS()) {\n"
+  "// if condition\n"
+  "  }\n"
   "  return r;\n"
   "}\n";
   std::string Postfix = "\n"
@@ -364,6 +376,9 @@
 "int bar3(int j, int k, const enum Bar b) {\n"
 "  // A comment\n"
 "  int r = j % k;\n"
+"  if (struct S = getS()) {\n"
+"// if condition\n"
+"  }\n"
 "  return r;\n"
 "}\n"
 "} // namespace";
@@ -425,6 +440,10 @@
"{\n"
"  // A comment\n"
"  int r = j % k;\n"
+   "  if (struct S = getS())\n"
+   "  {\n"
+   "// if condition\n"
+   "  }\n"
"  return r;\n"
"}\n"
"} // namespace NS",
@@ -473,6 +492,9 @@
 "int bar3(int j, int k, const enum Bar b) {\n"
 "  // A comment\n"
 "  int r = j % k;\n"
+"  if (struct S = getS()) {\n"
+"// if condition\n"
+"  }\n"
 "  return r;\n"
 "}\n"
 "} // namespace";
Index: clang/lib/Format/DefinitionBlockSeparator.cpp
===
--- clang/lib/Format/DefinitionBlockSeparator.cpp
+++ clang/lib/Format/DefinitionBlockSeparator.cpp
@@ -35,18 +35,31 @@
   const bool IsNeverStyle =
   Style.SeparateDefinitionBlocks == FormatStyle::SDS_Never;
   const AdditionalKeywords  = Tokens.getKeywords();
-  auto LikelyDefinition = [this, ExtraKeywords](const AnnotatedLine *Line,
-bool ExcludeEnum = false) {
+  auto GetBracketLevelChange = [](const FormatToken *Tok) {
+if (Tok->isOneOf(tok::l_brace, tok::l_paren, tok::l_square))
+  return 1;
+else if (Tok->isOneOf(tok::r_brace, tok::r_paren, tok::r_square))
+  return -1;
+return 0;
+  };
+  auto LikelyDefinition = [&](const AnnotatedLine *Line,
+  bool ExcludeEnum = false) {
 if ((Line->MightBeFunctionDecl && Line->mightBeFunctionDefinition()) ||
 Line->startsWithNamespace())
   return true;
 FormatToken *CurrentToken = Line->First;
+int BracketLevel = 0;
 while (CurrentToken) {
-  if (CurrentToken->isOneOf(tok::kw_class, tok::kw_struct) ||
-  (Style.isJavaScript() && CurrentToken->is(ExtraKeywords.kw_function)))
-return true;
-  if (!ExcludeEnum && CurrentToken->is(tok::kw_enum))
-return true;
+  if (BracketLevel == 0) {
+if ((CurrentToken->isOneOf(tok::kw_class, tok::kw_struct,
+   tok::kw_union) ||
+ (Style.isJavaScript() &&
+  CurrentToken->is(ExtraKeywords.kw_function
+  return true;
+if (!ExcludeEnum && CurrentToken->is(tok::kw_enum))
+  return true;
+  }
+  BracketLevel += GetBracketLevelChange(CurrentToken);
   CurrentToken = CurrentToken->Next;
 }
 return false;
@@ -104,11 +117,15 @@
 const auto HasEnumOnLine = [&]() {
   FormatToken *CurrentToken = CurrentLine->First;
   bool FoundEnumKeyword = false;
+  int BracketLevel = 0;
   while (CurrentToken) {
-if (CurrentToken->is(tok::kw_enum))
-  FoundEnumKeyword = true;
-else if (FoundEnumKeyword && CurrentToken->is(tok::l_brace))
-  

[PATCH] D119067: [clang-format] Fix DefinitionBlockSeparator extra empty lines

2022-02-05 Thread ksyx via Phabricator via cfe-commits
ksyx created this revision.
ksyx added reviewers: MyDeveloperDay, curdeius, HazardyKnusperkeks.
ksyx added a project: clang-format.
ksyx requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This commit fixes https://github.com/llvm/llvm-project/issues/53229, in
which keywords like class and struct in a line ending with left brace or
whose next line is left brace only, will be falsely recognized as
definition line, causing extra empty lines inserted surrounding blocks
with no need to be formatted.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D119067

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -302,6 +302,9 @@
   "\n"
   "int bar2(int j, int k) {\n"
   "  int r = j / k;\n"
+  "  if (struct S = getS()) {\n"
+  "// if condition\n"
+  "  }\n"
   "  return r;\n"
   "}\n"
   "\n"
@@ -355,6 +358,9 @@
 "\n"
 "int bar2(int j, int k) {\n"
 "  int r = j / k;\n"
+"  if (struct S = getS()) {\n"
+"// if condition\n"
+"  }\n"
 "  return r;\n"
 "}\n"
 "\n"
@@ -412,6 +418,10 @@
"int bar2(int j, int k)\n"
"{\n"
"  int r = j / k;\n"
+   "  if (struct S = getS())\n"
+   "  {\n"
+   "// if condition\n"
+   "  }\n"
"  return r;\n"
"}\n"
"\n"
@@ -465,6 +475,9 @@
 "\n"
 "int bar2(int j, int k) {\n"
 "  int r = j / k;\n"
+"  if (struct S = getS()) {\n"
+"// if condition\n"
+"  }\n"
 "  return r;\n"
 "}\n"
 "\n"
Index: clang/lib/Format/DefinitionBlockSeparator.cpp
===
--- clang/lib/Format/DefinitionBlockSeparator.cpp
+++ clang/lib/Format/DefinitionBlockSeparator.cpp
@@ -35,18 +35,31 @@
   const bool IsNeverStyle =
   Style.SeparateDefinitionBlocks == FormatStyle::SDS_Never;
   const AdditionalKeywords  = Tokens.getKeywords();
-  auto LikelyDefinition = [this, ExtraKeywords](const AnnotatedLine *Line,
-bool ExcludeEnum = false) {
+  auto GetBracketLevelChange = [](const FormatToken *Tok) {
+if (Tok->isOneOf(tok::l_brace, tok::l_paren, tok::l_square))
+  return 1;
+else if (Tok->isOneOf(tok::r_brace, tok::r_paren, tok::r_square))
+  return -1;
+else
+  return 0;
+  };
+  auto LikelyDefinition = [&](const AnnotatedLine *Line,
+  bool ExcludeEnum = false) {
 if ((Line->MightBeFunctionDecl && Line->mightBeFunctionDefinition()) ||
 Line->startsWithNamespace())
   return true;
 FormatToken *CurrentToken = Line->First;
+int BracketLevel = 0;
 while (CurrentToken) {
-  if (CurrentToken->isOneOf(tok::kw_class, tok::kw_struct) ||
-  (Style.isJavaScript() && CurrentToken->is(ExtraKeywords.kw_function)))
-return true;
-  if (!ExcludeEnum && CurrentToken->is(tok::kw_enum))
-return true;
+  if (BracketLevel == 0) {
+if ((CurrentToken->isOneOf(tok::kw_class, tok::kw_struct) ||
+ (Style.isJavaScript() &&
+  CurrentToken->is(ExtraKeywords.kw_function
+  return true;
+if (!ExcludeEnum && CurrentToken->is(tok::kw_enum))
+  return true;
+  }
+  BracketLevel += GetBracketLevelChange(CurrentToken);
   CurrentToken = CurrentToken->Next;
 }
 return false;
@@ -104,11 +117,15 @@
 const auto HasEnumOnLine = [&]() {
   FormatToken *CurrentToken = CurrentLine->First;
   bool FoundEnumKeyword = false;
+  int BracketLevel = 0;
   while (CurrentToken) {
-if (CurrentToken->is(tok::kw_enum))
-  FoundEnumKeyword = true;
-else if (FoundEnumKeyword && CurrentToken->is(tok::l_brace))
-  return true;
+if (BracketLevel == 0) {
+  if (CurrentToken->is(tok::kw_enum))
+FoundEnumKeyword = true;
+  else if (FoundEnumKeyword && CurrentToken->is(tok::l_brace))
+return true;
+}
+BracketLevel += 

[PATCH] D118869: [clang-format] Non-latin comment prefix whitespace

2022-02-03 Thread ksyx via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG88e4e6be16ea: [clang-format] Use wider comment prefix space 
rule (authored by ksyx).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118869/new/

https://reviews.llvm.org/D118869

Files:
  clang/lib/Format/BreakableToken.cpp
  clang/unittests/Format/FormatTestComments.cpp

Index: clang/unittests/Format/FormatTestComments.cpp
===
--- clang/unittests/Format/FormatTestComments.cpp
+++ clang/unittests/Format/FormatTestComments.cpp
@@ -3322,6 +3322,18 @@
   "\n"
   "///   Free Doxygen with 3 spaces\n"
   "\n"
+  "//🐉 A nice dragon\n"
+  "\n"
+  "//\t abccba\n"
+  "\n"
+  "//\\t deffed\n"
+  "\n"
+  "//   🐉 Another nice dragon\n"
+  "\n"
+  "//   \t Three leading spaces following tab\n"
+  "\n"
+  "//   \\t Three leading spaces following backslash\n"
+  "\n"
   "/// A Doxygen Comment with a nested list:\n"
   "/// - Foo\n"
   "/// - Bar\n"
@@ -3381,6 +3393,18 @@
 "\n"
 "///   Free Doxygen with 3 spaces\n"
 "\n"
+"// 🐉 A nice dragon\n"
+"\n"
+"//\t abccba\n"
+"\n"
+"//\\t deffed\n"
+"\n"
+"//   🐉 Another nice dragon\n"
+"\n"
+"//   \t Three leading spaces following tab\n"
+"\n"
+"//   \\t Three leading spaces following backslash\n"
+"\n"
 "/// A Doxygen Comment with a nested list:\n"
 "/// - Foo\n"
 "/// - Bar\n"
@@ -3442,6 +3466,18 @@
 "\n"
 "///Free Doxygen with 3 spaces\n"
 "\n"
+"//🐉 A nice dragon\n"
+"\n"
+"//\t abccba\n"
+"\n"
+"//\\t deffed\n"
+"\n"
+"//🐉 Another nice dragon\n"
+"\n"
+"//\t Three leading spaces following tab\n"
+"\n"
+"//\\t Three leading spaces following backslash\n"
+"\n"
 "///A Doxygen Comment with a nested list:\n"
 "///- Foo\n"
 "///- Bar\n"
@@ -3503,6 +3539,18 @@
 "\n"
 "///   Free Doxygen with 3 spaces\n"
 "\n"
+"//  🐉 A nice dragon\n"
+"\n"
+"//\t abccba\n"
+"\n"
+"//\\t deffed\n"
+"\n"
+"//   🐉 Another nice dragon\n"
+"\n"
+"//   \t Three leading spaces following tab\n"
+"\n"
+"//   \\t Three leading spaces following backslash\n"
+"\n"
 "///  A Doxygen Comment with a nested list:\n"
 "///  - Foo\n"
 "///  - Bar\n"
@@ -3809,6 +3857,18 @@
 "\n"
 "///   Free Doxygen with 3 spaces\n"
 "\n"
+"// 🐉 A nice dragon\n"
+"\n"
+"//\t abccba\n"
+"\n"
+"//\\t deffed\n"
+"\n"
+"//   🐉 Another nice dragon\n"
+"\n"
+"//   \t Three leading spaces following tab\n"
+"\n"
+"//   \\t Three leading spaces following backslash\n"
+"\n"
 "/// A Doxygen Comment with a nested list:\n"
 "/// - Foo\n"
 "/// - Bar\n"
@@ -3870,6 +3930,18 @@
 "\n"
 "///Free Doxygen with 3 spaces\n"
 "\n"
+"//🐉 A nice dragon\n"
+"\n"
+"//\t abccba\n"
+"\n"
+"//\\t deffed\n"
+"\n"
+"//🐉 Another nice dragon\n"
+"\n"
+"//\t Three leading spaces following tab\n"
+"\n"
+"//\\t Three leading spaces following backslash\n"
+"\n"
 "///A Doxygen Comment with a nested list:\n"
 "///- Foo\n"
 "///- Bar\n"
@@ -3931,6 +4003,18 @@
 "\n"
 "///   Free Doxygen with 3 spaces\n"
 "\n"
+"//  🐉 A nice dragon\n"
+"\n"
+"//\t abccba\n"
+"\n"
+"//\\t deffed\n"
+"\n"
+"//   🐉 Another nice dragon\n"
+"\n"
+"//   \t Three leading spaces following tab\n"
+"\n"
+"//   \\t Three leading spaces following backslash\n"
+"\n"
 "///  A Doxygen Comment with a nested list:\n"
 "///  - Foo\n"
 "///  - Bar\n"
Index: clang/lib/Format/BreakableToken.cpp
===
--- clang/lib/Format/BreakableToken.cpp
+++ clang/lib/Format/BreakableToken.cpp
@@ -771,6 +771,20 @@
   

[PATCH] D118869: [clang-format] Non-latin comment prefix whitespace

2022-02-02 Thread ksyx via Phabricator via cfe-commits
ksyx added subscribers: byronhe, CoelacanthusHex.
ksyx added a comment.

Related discussion: https://reviews.llvm.org/D92257#3252060

Proposed by @byronhe

cc @CoelacanthusHex


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118869/new/

https://reviews.llvm.org/D118869

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


[PATCH] D118869: [clang-format] Non-latin comment prefix whitespace

2022-02-02 Thread ksyx via Phabricator via cfe-commits
ksyx created this revision.
ksyx added reviewers: HazardyKnusperkeks, MyDeveloperDay, curdeius.
ksyx requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This commit changes the condition of requiring comment to start with
alphabet characters to make no change only for a certain set of
characters, currently horizontal whitespace and punctuation characters.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118869

Files:
  clang/lib/Format/BreakableToken.cpp
  clang/unittests/Format/FormatTestComments.cpp

Index: clang/unittests/Format/FormatTestComments.cpp
===
--- clang/unittests/Format/FormatTestComments.cpp
+++ clang/unittests/Format/FormatTestComments.cpp
@@ -3322,6 +3322,18 @@
   "\n"
   "///   Free Doxygen with 3 spaces\n"
   "\n"
+  "//🐉 A nice dragon\n"
+  "\n"
+  "//\t abccba\n"
+  "\n"
+  "//\\t deffed\n"
+  "\n"
+  "//   🐉 Another nice dragon\n"
+  "\n"
+  "//   \t Three leading spaces following tab\n"
+  "\n"
+  "//   \\t Three leading spaces following backslash\n"
+  "\n"
   "/// A Doxygen Comment with a nested list:\n"
   "/// - Foo\n"
   "/// - Bar\n"
@@ -3381,6 +3393,18 @@
 "\n"
 "///   Free Doxygen with 3 spaces\n"
 "\n"
+"// 🐉 A nice dragon\n"
+"\n"
+"//\t abccba\n"
+"\n"
+"//\\t deffed\n"
+"\n"
+"//   🐉 Another nice dragon\n"
+"\n"
+"//   \t Three leading spaces following tab\n"
+"\n"
+"//   \\t Three leading spaces following backslash\n"
+"\n"
 "/// A Doxygen Comment with a nested list:\n"
 "/// - Foo\n"
 "/// - Bar\n"
@@ -3442,6 +3466,18 @@
 "\n"
 "///Free Doxygen with 3 spaces\n"
 "\n"
+"//🐉 A nice dragon\n"
+"\n"
+"//\t abccba\n"
+"\n"
+"//\\t deffed\n"
+"\n"
+"//🐉 Another nice dragon\n"
+"\n"
+"//\t Three leading spaces following tab\n"
+"\n"
+"//\\t Three leading spaces following backslash\n"
+"\n"
 "///A Doxygen Comment with a nested list:\n"
 "///- Foo\n"
 "///- Bar\n"
@@ -3503,6 +3539,18 @@
 "\n"
 "///   Free Doxygen with 3 spaces\n"
 "\n"
+"//  🐉 A nice dragon\n"
+"\n"
+"//\t abccba\n"
+"\n"
+"//\\t deffed\n"
+"\n"
+"//   🐉 Another nice dragon\n"
+"\n"
+"//   \t Three leading spaces following tab\n"
+"\n"
+"//   \\t Three leading spaces following backslash\n"
+"\n"
 "///  A Doxygen Comment with a nested list:\n"
 "///  - Foo\n"
 "///  - Bar\n"
@@ -3809,6 +3857,18 @@
 "\n"
 "///   Free Doxygen with 3 spaces\n"
 "\n"
+"// 🐉 A nice dragon\n"
+"\n"
+"//\t abccba\n"
+"\n"
+"//\\t deffed\n"
+"\n"
+"//   🐉 Another nice dragon\n"
+"\n"
+"//   \t Three leading spaces following tab\n"
+"\n"
+"//   \\t Three leading spaces following backslash\n"
+"\n"
 "/// A Doxygen Comment with a nested list:\n"
 "/// - Foo\n"
 "/// - Bar\n"
@@ -3870,6 +3930,18 @@
 "\n"
 "///Free Doxygen with 3 spaces\n"
 "\n"
+"//🐉 A nice dragon\n"
+"\n"
+"//\t abccba\n"
+"\n"
+"//\\t deffed\n"
+"\n"
+"//🐉 Another nice dragon\n"
+"\n"
+"//\t Three leading spaces following tab\n"
+"\n"
+"//\\t Three leading spaces following backslash\n"
+"\n"
 "///A Doxygen Comment with a nested list:\n"
 "///- Foo\n"
 "///- Bar\n"
@@ -3931,6 +4003,18 @@
 "\n"
 "///   Free Doxygen with 3 spaces\n"
 "\n"
+"//  🐉 A nice dragon\n"
+"\n"
+"//\t abccba\n"
+"\n"
+"//\\t deffed\n"
+"\n"
+"//   🐉 Another nice dragon\n"
+"\n"
+"//   \t Three leading spaces following tab\n"
+"\n"
+"//   \\t Three leading spaces following backslash\n"
+"\n"
 "///  A Doxygen Comment with a nested list:\n"
 "///  - Foo\n"
 "///  - Bar\n"
Index: clang/lib/Format/BreakableToken.cpp
===
--- 

[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-24 Thread ksyx via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
ksyx marked 2 inline comments as done.
Closed by commit rG5e5efd8a91f2: [clang-format] Fix SeparateDefinitionBlocks 
issues (authored by ksyx).

Changed prior to commit:
  https://reviews.llvm.org/D117520?vs=402078=402498#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/FormatToken.h
  clang/lib/Format/TokenAnnotator.cpp
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -131,6 +131,73 @@
"\n"
"enum Bar { FOOBAR, BARFOO };\n",
Style);
+
+  FormatStyle BreakAfterReturnTypeStyle = Style;
+  BreakAfterReturnTypeStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
+  // Test uppercased long typename
+  verifyFormat("class Foo {\n"
+   "  void\n"
+   "  Bar(int t, int p) {\n"
+   "int r = t + p;\n"
+   "return r;\n"
+   "  }\n"
+   "\n"
+   "  HRESULT\n"
+   "  Foobar(int t, int p) {\n"
+   "int r = t * p;\n"
+   "return r;\n"
+   "  }\n"
+   "}\n",
+   BreakAfterReturnTypeStyle);
+}
+
+TEST_F(DefinitionBlockSeparatorTest, FormatConflict) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  llvm::StringRef Code = "class Test {\n"
+ "public:\n"
+ "  static void foo() {\n"
+ "int t;\n"
+ "return 1;\n"
+ "  }\n"
+ "};";
+  std::vector Ranges = {1, tooling::Range(0, Code.size())};
+  EXPECT_EQ(reformat(Style, Code, Ranges, "").size(), 0u);
+}
+
+TEST_F(DefinitionBlockSeparatorTest, CommentBlock) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  std::string Prefix = "enum Foo { FOO, BAR };\n"
+   "\n"
+   "/*\n"
+   "test1\n"
+   "test2\n"
+   "*/\n"
+   "int foo(int i, int j) {\n"
+   "  int r = i + j;\n"
+   "  return r;\n"
+   "}\n";
+  std::string Suffix = "enum Bar { FOOBAR, BARFOO };\n"
+   "\n"
+   "/* Comment block in one line*/\n"
+   "int bar3(int j, int k) {\n"
+   "  // A comment\n"
+   "  int r = j % k;\n"
+   "  return r;\n"
+   "}\n";
+  std::string CommentedCode = "/*\n"
+  "int bar2(int j, int k) {\n"
+  "  int r = j / k;\n"
+  "  return r;\n"
+  "}\n"
+  "*/\n";
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode + "\n" +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + "\n" + Suffix);
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + Suffix);
 }
 
 TEST_F(DefinitionBlockSeparatorTest, UntouchBlockStartStyle) {
@@ -175,13 +242,15 @@
   FormatStyle NeverStyle = getLLVMStyle();
   NeverStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
 
-  auto TestKit = MakeUntouchTest("#ifdef FOO\n\n", "\n#elifndef BAR\n\n",
- "\n#endif\n\n", false);
+  auto TestKit = MakeUntouchTest("/* FOOBAR */\n"
+ "#ifdef FOO\n\n",
+ "\n#elifndef BAR\n\n", "\n#endif\n\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
-  TestKit =
-  MakeUntouchTest("#ifdef FOO\n", "#elifndef BAR\n", "#endif\n", false);
+  TestKit = MakeUntouchTest("/* FOOBAR */\n"
+"#ifdef FOO\n",
+"#elifndef BAR\n", "#endif\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
@@ -213,7 +282,7 @@
   "test1\n"
   "test2\n"
   "*/\n"
- 

[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-22 Thread ksyx via Phabricator via cfe-commits
ksyx marked 2 inline comments as done.
ksyx added a comment.

In D117520#3263403 , @MyDeveloperDay 
wrote:

> maybe slightly related https://github.com/llvm/llvm-project/issues/53183 in 
> that this is also impacted by the 5 character 
> `TT_FunctionLikeOrFreestandingMacro`

Yes this patch is just marking this type of recognition but surely it requires 
other changes to the formatter.

By the way are there any improvements needed for this patch? Thanks.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

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


[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-21 Thread ksyx via Phabricator via cfe-commits
ksyx added inline comments.



Comment at: clang/lib/Format/DefinitionBlockSeparator.cpp:134
+  OperateIndex + 1 < Lines.size()) {
+// UnwrappedLineParser's recognition of free-standing macro like
+// Q_OBJECT may also recognize some uppercased type names that may be

HazardyKnusperkeks wrote:
> curdeius wrote:
> > ksyx wrote:
> > > HazardyKnusperkeks wrote:
> > > > Shouldn't we set a type for such cases instead of repeating the 
> > > > detection code here?
> > > Here I actually did a few more checks to limit the impact to the minimum 
> > > but I am happy to do that if that's necessary.
> > > Shouldn't we set a type for such cases instead of repeating the detection 
> > > code here?
> > 
> > :+1:
> Thanks! Shouldn't we drop are radically change this comment?
I've checked this comment but found nothing to change, since it has nothing to 
do with the duplicated check but just referring to where created this problem?



Comment at: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp:143
+  "\n"
+  "  LONGTYPENAME\n"
+  "  Foobar(int t, int p) {\n"

HazardyKnusperkeks wrote:
> ksyx wrote:
> > HazardyKnusperkeks wrote:
> > > Maybe really use HRESULT? People will know what that should be.
> > It actually does not matter what type name it is as long as it is an 
> > identifier with >=5 characters and all uppercased
> That I know, but HRESULT would look familiar to at least some people.
This makes sense! Thanks for the suggestion.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

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


[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-21 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 402078.
ksyx marked an inline comment as done.
ksyx added a comment.

Apply review suggestions of renaming.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/FormatToken.h
  clang/lib/Format/TokenAnnotator.cpp
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -131,6 +131,73 @@
"\n"
"enum Bar { FOOBAR, BARFOO };\n",
Style);
+
+  FormatStyle BreakAfterReturnTypeStyle = Style;
+  BreakAfterReturnTypeStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
+  // Test uppercased long typename
+  verifyFormat("class Foo {\n"
+   "  void\n"
+   "  Bar(int t, int p) {\n"
+   "int r = t + p;\n"
+   "return r;\n"
+   "  }\n"
+   "\n"
+   "  HRESULT\n"
+   "  Foobar(int t, int p) {\n"
+   "int r = t * p;\n"
+   "return r;\n"
+   "  }\n"
+   "}\n",
+   BreakAfterReturnTypeStyle);
+}
+
+TEST_F(DefinitionBlockSeparatorTest, FormatConflict) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  llvm::StringRef Code = "class Test {\n"
+ "public:\n"
+ "  static void foo() {\n"
+ "int t;\n"
+ "return 1;\n"
+ "  }\n"
+ "};";
+  std::vector Ranges = {1, tooling::Range(0, Code.size())};
+  EXPECT_EQ(reformat(Style, Code, Ranges, "").size(), 0u);
+}
+
+TEST_F(DefinitionBlockSeparatorTest, CommentBlock) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  std::string Prefix = "enum Foo { FOO, BAR };\n"
+   "\n"
+   "/*\n"
+   "test1\n"
+   "test2\n"
+   "*/\n"
+   "int foo(int i, int j) {\n"
+   "  int r = i + j;\n"
+   "  return r;\n"
+   "}\n";
+  std::string Suffix = "enum Bar { FOOBAR, BARFOO };\n"
+   "\n"
+   "/* Comment block in one line*/\n"
+   "int bar3(int j, int k) {\n"
+   "  // A comment\n"
+   "  int r = j % k;\n"
+   "  return r;\n"
+   "}\n";
+  std::string CommentedCode = "/*\n"
+  "int bar2(int j, int k) {\n"
+  "  int r = j / k;\n"
+  "  return r;\n"
+  "}\n"
+  "*/\n";
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode + "\n" +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + "\n" + Suffix);
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + Suffix);
 }
 
 TEST_F(DefinitionBlockSeparatorTest, UntouchBlockStartStyle) {
@@ -175,13 +242,15 @@
   FormatStyle NeverStyle = getLLVMStyle();
   NeverStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
 
-  auto TestKit = MakeUntouchTest("#ifdef FOO\n\n", "\n#elifndef BAR\n\n",
- "\n#endif\n\n", false);
+  auto TestKit = MakeUntouchTest("/* FOOBAR */\n"
+ "#ifdef FOO\n\n",
+ "\n#elifndef BAR\n\n", "\n#endif\n\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
-  TestKit =
-  MakeUntouchTest("#ifdef FOO\n", "#elifndef BAR\n", "#endif\n", false);
+  TestKit = MakeUntouchTest("/* FOOBAR */\n"
+"#ifdef FOO\n",
+"#elifndef BAR\n", "#endif\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
@@ -213,7 +282,7 @@
   "test1\n"
   "test2\n"
   "*/\n"
-  "int foo(int i, int j) {\n"
+  "/*const*/ int foo(int i, int j) {\n"
   "  int r = i + j;\n"
   "  return r;\n"
  

[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-21 Thread ksyx via Phabricator via cfe-commits
ksyx marked an inline comment as done.
ksyx added inline comments.



Comment at: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp:47
 llvm::StringRef ExpectedCode = "") {
 ::testing::ScopedTrace t(File, Line, ::testing::Message() << Code.str());
 bool HasOriginalCode = true;

HazardyKnusperkeks wrote:
> This is from Debugging?
I suppose it is from rebasing.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

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


[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-21 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 402006.
ksyx edited the summary of this revision.
ksyx added a comment.

Add token type `FunctionLikeOrFreestandingMacro` and use it to replace 
duplicated check with UnwrappedLineParser


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/FormatToken.h
  clang/lib/Format/TokenAnnotator.cpp
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -131,6 +131,73 @@
"\n"
"enum Bar { FOOBAR, BARFOO };\n",
Style);
+
+  FormatStyle BreakAfterReturnTypeStyle = Style;
+  BreakAfterReturnTypeStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
+  // Test uppercased long typename
+  verifyFormat("class Foo {\n"
+   "  void\n"
+   "  Bar(int t, int p) {\n"
+   "int r = t + p;\n"
+   "return r;\n"
+   "  }\n"
+   "\n"
+   "  LONGTYPENAME\n"
+   "  Foobar(int t, int p) {\n"
+   "int r = t * p;\n"
+   "return r;\n"
+   "  }\n"
+   "}\n",
+   BreakAfterReturnTypeStyle);
+}
+
+TEST_F(DefinitionBlockSeparatorTest, FormatConflict) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  llvm::StringRef Code = "class Test {\n"
+ "public:\n"
+ "  static void foo() {\n"
+ "int t;\n"
+ "return 1;\n"
+ "  }\n"
+ "};";
+  std::vector Ranges = {1, tooling::Range(0, Code.size())};
+  EXPECT_EQ(reformat(Style, Code, Ranges, "").size(), 0u);
+}
+
+TEST_F(DefinitionBlockSeparatorTest, CommentBlock) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  std::string Prefix = "enum Foo { FOO, BAR };\n"
+   "\n"
+   "/*\n"
+   "test1\n"
+   "test2\n"
+   "*/\n"
+   "int foo(int i, int j) {\n"
+   "  int r = i + j;\n"
+   "  return r;\n"
+   "}\n";
+  std::string Suffix = "enum Bar { FOOBAR, BARFOO };\n"
+   "\n"
+   "/* Comment block in one line*/\n"
+   "int bar3(int j, int k) {\n"
+   "  // A comment\n"
+   "  int r = j % k;\n"
+   "  return r;\n"
+   "}\n";
+  std::string CommentedCode = "/*\n"
+  "int bar2(int j, int k) {\n"
+  "  int r = j / k;\n"
+  "  return r;\n"
+  "}\n"
+  "*/\n";
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode + "\n" +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + "\n" + Suffix);
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + Suffix);
 }
 
 TEST_F(DefinitionBlockSeparatorTest, UntouchBlockStartStyle) {
@@ -175,13 +242,15 @@
   FormatStyle NeverStyle = getLLVMStyle();
   NeverStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
 
-  auto TestKit = MakeUntouchTest("#ifdef FOO\n\n", "\n#elifndef BAR\n\n",
- "\n#endif\n\n", false);
+  auto TestKit = MakeUntouchTest("/* FOOBAR */\n"
+ "#ifdef FOO\n\n",
+ "\n#elifndef BAR\n\n", "\n#endif\n\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
-  TestKit =
-  MakeUntouchTest("#ifdef FOO\n", "#elifndef BAR\n", "#endif\n", false);
+  TestKit = MakeUntouchTest("/* FOOBAR */\n"
+"#ifdef FOO\n",
+"#elifndef BAR\n", "#endif\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
@@ -213,7 +282,7 @@
   "test1\n"
   "test2\n"
   "*/\n"
-  "int foo(int i, int j) {\n"
+  "/*const*/ int foo(int i, int j) {\n"
 

[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-21 Thread ksyx via Phabricator via cfe-commits
ksyx added a comment.

In D117520#3260443 , @curdeius wrote:

> In D117520#3258454 , 
> @MyDeveloperDay wrote:
>
>> I think its been fixed, in the last but one diff.
>>
>> Generally speaking we simply cannot have --output-replacements-xml , -n or 
>> --dry-run show replacements that amount to nothing because this is the 
>> mechanism by which 100s of repos fail commits thinking their is a 
>> clang-format issue.
>>
>> Last revision I tried these issues seem to have disappeared
>
> I understood that it was fixed but I'd really like to have a test case that 
> reproduces it.
> We might have the same problem somewhere else, no?
> Can we have a part of `verifyFormat` that runs with `DryRun=true` and checks 
> the replacements?

Yes I see your point so I added one in the last diff.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

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


[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-20 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 401659.
ksyx added a comment.

Add unit test for formatting conflict


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -128,6 +128,73 @@
"\n"
"enum Bar { FOOBAR, BARFOO };\n",
Style);
+
+  FormatStyle BreakAfterReturnTypeStyle = Style;
+  BreakAfterReturnTypeStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
+  // Test uppercased long typename
+  verifyFormat("class Foo {\n"
+   "  void\n"
+   "  Bar(int t, int p) {\n"
+   "int r = t + p;\n"
+   "return r;\n"
+   "  }\n"
+   "\n"
+   "  LONGTYPENAME\n"
+   "  Foobar(int t, int p) {\n"
+   "int r = t * p;\n"
+   "return r;\n"
+   "  }\n"
+   "}\n",
+   BreakAfterReturnTypeStyle);
+}
+
+TEST_F(DefinitionBlockSeparatorTest, FormatConflict) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  llvm::StringRef Code = "class Test {\n"
+ "public:\n"
+ "  static void foo() {\n"
+ "int t;\n"
+ "return 1;\n"
+ "  }\n"
+ "};";
+  std::vector Ranges = {1, tooling::Range(0, Code.size())};
+  EXPECT_EQ(reformat(Style, Code, Ranges, "").size(), 0u);
+}
+
+TEST_F(DefinitionBlockSeparatorTest, CommentBlock) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  std::string Prefix = "enum Foo { FOO, BAR };\n"
+   "\n"
+   "/*\n"
+   "test1\n"
+   "test2\n"
+   "*/\n"
+   "int foo(int i, int j) {\n"
+   "  int r = i + j;\n"
+   "  return r;\n"
+   "}\n";
+  std::string Suffix = "enum Bar { FOOBAR, BARFOO };\n"
+   "\n"
+   "/* Comment block in one line*/\n"
+   "int bar3(int j, int k) {\n"
+   "  // A comment\n"
+   "  int r = j % k;\n"
+   "  return r;\n"
+   "}\n";
+  std::string CommentedCode = "/*\n"
+  "int bar2(int j, int k) {\n"
+  "  int r = j / k;\n"
+  "  return r;\n"
+  "}\n"
+  "*/\n";
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode + "\n" +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + "\n" + Suffix);
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + Suffix);
 }
 
 TEST_F(DefinitionBlockSeparatorTest, UntouchBlockStartStyle) {
@@ -172,13 +239,15 @@
   FormatStyle NeverStyle = getLLVMStyle();
   NeverStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
 
-  auto TestKit = MakeUntouchTest("#ifdef FOO\n\n", "\n#elifndef BAR\n\n",
- "\n#endif\n\n", false);
+  auto TestKit = MakeUntouchTest("/* FOOBAR */\n"
+ "#ifdef FOO\n\n",
+ "\n#elifndef BAR\n\n", "\n#endif\n\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
-  TestKit =
-  MakeUntouchTest("#ifdef FOO\n", "#elifndef BAR\n", "#endif\n", false);
+  TestKit = MakeUntouchTest("/* FOOBAR */\n"
+"#ifdef FOO\n",
+"#elifndef BAR\n", "#endif\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
@@ -210,7 +279,7 @@
   "test1\n"
   "test2\n"
   "*/\n"
-  "int foo(int i, int j) {\n"
+  "/*const*/ int foo(int i, int j) {\n"
   "  int r = i + j;\n"
   "  return r;\n"
   "}\n"
@@ -222,8 +291,10 @@
   "// Comment line 2\n"
   "// Comment line 3\n"
   "int 

[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-19 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 401460.
ksyx added a comment.

Apply suggestion from clangfmt


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -128,6 +128,59 @@
"\n"
"enum Bar { FOOBAR, BARFOO };\n",
Style);
+
+  FormatStyle BreakAfterReturnTypeStyle = Style;
+  BreakAfterReturnTypeStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
+  // Test uppercased long typename
+  verifyFormat("class Foo {\n"
+   "  void\n"
+   "  Bar(int t, int p) {\n"
+   "int r = t + p;\n"
+   "return r;\n"
+   "  }\n"
+   "\n"
+   "  LONGTYPENAME\n"
+   "  Foobar(int t, int p) {\n"
+   "int r = t * p;\n"
+   "return r;\n"
+   "  }\n"
+   "}\n",
+   BreakAfterReturnTypeStyle);
+}
+
+TEST_F(DefinitionBlockSeparatorTest, CommentBlock) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  std::string Prefix = "enum Foo { FOO, BAR };\n"
+   "\n"
+   "/*\n"
+   "test1\n"
+   "test2\n"
+   "*/\n"
+   "int foo(int i, int j) {\n"
+   "  int r = i + j;\n"
+   "  return r;\n"
+   "}\n";
+  std::string Suffix = "enum Bar { FOOBAR, BARFOO };\n"
+   "\n"
+   "/* Comment block in one line*/\n"
+   "int bar3(int j, int k) {\n"
+   "  // A comment\n"
+   "  int r = j % k;\n"
+   "  return r;\n"
+   "}\n";
+  std::string CommentedCode = "/*\n"
+  "int bar2(int j, int k) {\n"
+  "  int r = j / k;\n"
+  "  return r;\n"
+  "}\n"
+  "*/\n";
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode + "\n" +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + "\n" + Suffix);
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + Suffix);
 }
 
 TEST_F(DefinitionBlockSeparatorTest, UntouchBlockStartStyle) {
@@ -172,13 +225,15 @@
   FormatStyle NeverStyle = getLLVMStyle();
   NeverStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
 
-  auto TestKit = MakeUntouchTest("#ifdef FOO\n\n", "\n#elifndef BAR\n\n",
- "\n#endif\n\n", false);
+  auto TestKit = MakeUntouchTest("/* FOOBAR */\n"
+ "#ifdef FOO\n\n",
+ "\n#elifndef BAR\n\n", "\n#endif\n\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
-  TestKit =
-  MakeUntouchTest("#ifdef FOO\n", "#elifndef BAR\n", "#endif\n", false);
+  TestKit = MakeUntouchTest("/* FOOBAR */\n"
+"#ifdef FOO\n",
+"#elifndef BAR\n", "#endif\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
@@ -210,7 +265,7 @@
   "test1\n"
   "test2\n"
   "*/\n"
-  "int foo(int i, int j) {\n"
+  "/*const*/ int foo(int i, int j) {\n"
   "  int r = i + j;\n"
   "  return r;\n"
   "}\n"
@@ -222,8 +277,10 @@
   "// Comment line 2\n"
   "// Comment line 3\n"
   "int bar(int j, int k) {\n"
-  "  int r = j * k;\n"
-  "  return r;\n"
+  "  {\n"
+  "int r = j * k;\n"
+  "return r;\n"
+  "  }\n"
   "}\n"
   "\n"
   "int bar2(int j, int k) {\n"
@@ -234,7 +291,7 @@
   "/* Comment block in one line*/\n"
   "enum Bar { FOOBAR, BARFOO };\n"
   "\n"
-  "int bar3(int j, int k) 

[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-19 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 401445.
ksyx marked an inline comment as done.
ksyx added a comment.

- Apply suggestions from clangfmt
- Split assertion conditions


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -128,6 +128,59 @@
"\n"
"enum Bar { FOOBAR, BARFOO };\n",
Style);
+
+  FormatStyle BreakAfterReturnTypeStyle = Style;
+  BreakAfterReturnTypeStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
+  // Test uppercased long typename
+  verifyFormat("class Foo {\n"
+   "  void\n"
+   "  Bar(int t, int p) {\n"
+   "int r = t + p;\n"
+   "return r;\n"
+   "  }\n"
+   "\n"
+   "  LONGTYPENAME\n"
+   "  Foobar(int t, int p) {\n"
+   "int r = t * p;\n"
+   "return r;\n"
+   "  }\n"
+   "}\n",
+  BreakAfterReturnTypeStyle);
+}
+
+TEST_F(DefinitionBlockSeparatorTest, CommentBlock) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  std::string Prefix = "enum Foo { FOO, BAR };\n"
+   "\n"
+   "/*\n"
+   "test1\n"
+   "test2\n"
+   "*/\n"
+   "int foo(int i, int j) {\n"
+   "  int r = i + j;\n"
+   "  return r;\n"
+   "}\n";
+  std::string Suffix = "enum Bar { FOOBAR, BARFOO };\n"
+   "\n"
+   "/* Comment block in one line*/\n"
+   "int bar3(int j, int k) {\n"
+   "  // A comment\n"
+   "  int r = j % k;\n"
+   "  return r;\n"
+   "}\n";
+  std::string CommentedCode = "/*\n"
+  "int bar2(int j, int k) {\n"
+  "  int r = j / k;\n"
+  "  return r;\n"
+  "}\n"
+  "*/\n";
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode + "\n" +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + "\n" + Suffix);
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + Suffix);
 }
 
 TEST_F(DefinitionBlockSeparatorTest, UntouchBlockStartStyle) {
@@ -172,13 +225,15 @@
   FormatStyle NeverStyle = getLLVMStyle();
   NeverStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
 
-  auto TestKit = MakeUntouchTest("#ifdef FOO\n\n", "\n#elifndef BAR\n\n",
- "\n#endif\n\n", false);
+  auto TestKit = MakeUntouchTest("/* FOOBAR */\n"
+ "#ifdef FOO\n\n",
+ "\n#elifndef BAR\n\n", "\n#endif\n\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
-  TestKit =
-  MakeUntouchTest("#ifdef FOO\n", "#elifndef BAR\n", "#endif\n", false);
+  TestKit = MakeUntouchTest("/* FOOBAR */\n"
+"#ifdef FOO\n",
+"#elifndef BAR\n", "#endif\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
@@ -210,7 +265,7 @@
   "test1\n"
   "test2\n"
   "*/\n"
-  "int foo(int i, int j) {\n"
+  "/*const*/ int foo(int i, int j) {\n"
   "  int r = i + j;\n"
   "  return r;\n"
   "}\n"
@@ -222,8 +277,10 @@
   "// Comment line 2\n"
   "// Comment line 3\n"
   "int bar(int j, int k) {\n"
-  "  int r = j * k;\n"
-  "  return r;\n"
+  "  {\n"
+  "int r = j * k;\n"
+  "return r;\n"
+  "  }\n"
   "}\n"
   "\n"
   "int bar2(int j, int k) {\n"
@@ -234,7 +291,7 @@
   "/* Comment block in one line*/\n"
   "enum Bar { FOOBAR, BARFOO };\n"
   

[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-19 Thread ksyx via Phabricator via cfe-commits
ksyx added inline comments.



Comment at: clang/lib/Format/DefinitionBlockSeparator.cpp:134
+  OperateIndex + 1 < Lines.size()) {
+// UnwrappedLineParser's recognition of free-standing macro like
+// Q_OBJECT may also recognize some uppercased type names that may be

HazardyKnusperkeks wrote:
> Shouldn't we set a type for such cases instead of repeating the detection 
> code here?
Here I actually did a few more checks to limit the impact to the minimum but I 
am happy to do that if that's necessary.



Comment at: clang/lib/Format/DefinitionBlockSeparator.cpp:135
+// UnwrappedLineParser's recognition of free-standing macro like
+// Q_OBJECT may also recognize some uppercased type names that may be
+// used as return type as that kind of macros, which is a bit hard to

HazardyKnusperkeks wrote:
> As a Qt user who also wants to use your patch, please add a test for that 
> case. ;)
For `Q_OBJECT` if used as pattern like this:
```
class X : ... {
  Q_OBJECT
public:
  // ...
}
```
I think this patch has no effect on this? The comment here is just a repetition 
of that in unwrapped parser



Comment at: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp:143
+  "\n"
+  "  LONGTYPENAME\n"
+  "  Foobar(int t, int p) {\n"

HazardyKnusperkeks wrote:
> Maybe really use HRESULT? People will know what that should be.
It actually does not matter what type name it is as long as it is an identifier 
with >=5 characters and all uppercased


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

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


[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-19 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 401293.
ksyx edited the summary of this revision.
ksyx added a comment.

Recognize long (len>=5) uppercased name taking a single line as return type
and fix the problem of adding newline below it:

  void
  afunc(int x) {
return;
  }
  TYPENAME
  func(int x, int y) {
// ...
  }


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -128,6 +128,60 @@
"\n"
"enum Bar { FOOBAR, BARFOO };\n",
Style);
+
+  FormatStyle BreakAfterReturnTypeStyle = Style;
+  BreakAfterReturnTypeStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
+  // Test uppercased long typename
+  verifyFormat(
+  "class Foo {\n"
+  "  void\n"
+  "  Bar(int t, int p) {\n"
+  "int r = t + p;\n"
+  "return r;\n"
+  "  }\n"
+  "\n"
+  "  LONGTYPENAME\n"
+  "  Foobar(int t, int p) {\n"
+  "int r = t * p;\n"
+  "return r;\n"
+  "  }\n"
+  "}\n",
+  BreakAfterReturnTypeStyle);
+}
+
+TEST_F(DefinitionBlockSeparatorTest, CommentBlock) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  std::string Prefix = "enum Foo { FOO, BAR };\n"
+   "\n"
+   "/*\n"
+   "test1\n"
+   "test2\n"
+   "*/\n"
+   "int foo(int i, int j) {\n"
+   "  int r = i + j;\n"
+   "  return r;\n"
+   "}\n";
+  std::string Suffix = "enum Bar { FOOBAR, BARFOO };\n"
+   "\n"
+   "/* Comment block in one line*/\n"
+   "int bar3(int j, int k) {\n"
+   "  // A comment\n"
+   "  int r = j % k;\n"
+   "  return r;\n"
+   "}\n";
+  std::string CommentedCode = "/*\n"
+  "int bar2(int j, int k) {\n"
+  "  int r = j / k;\n"
+  "  return r;\n"
+  "}\n"
+  "*/\n";
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode + "\n" +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + "\n" + Suffix);
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + Suffix);
 }
 
 TEST_F(DefinitionBlockSeparatorTest, UntouchBlockStartStyle) {
@@ -172,13 +226,15 @@
   FormatStyle NeverStyle = getLLVMStyle();
   NeverStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
 
-  auto TestKit = MakeUntouchTest("#ifdef FOO\n\n", "\n#elifndef BAR\n\n",
- "\n#endif\n\n", false);
+  auto TestKit = MakeUntouchTest("/* FOOBAR */\n"
+ "#ifdef FOO\n\n",
+ "\n#elifndef BAR\n\n", "\n#endif\n\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
-  TestKit =
-  MakeUntouchTest("#ifdef FOO\n", "#elifndef BAR\n", "#endif\n", false);
+  TestKit = MakeUntouchTest("/* FOOBAR */\n"
+"#ifdef FOO\n",
+"#elifndef BAR\n", "#endif\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
@@ -210,7 +266,7 @@
   "test1\n"
   "test2\n"
   "*/\n"
-  "int foo(int i, int j) {\n"
+  "/*const*/ int foo(int i, int j) {\n"
   "  int r = i + j;\n"
   "  return r;\n"
   "}\n"
@@ -222,8 +278,10 @@
   "// Comment line 2\n"
   "// Comment line 3\n"
   "int bar(int j, int k) {\n"
-  "  int r = j * k;\n"
-  "  return r;\n"
+  "  {\n"
+  "int r = j * k;\n"
+  "return r;\n"
+  "  }\n"
   "}\n"
   "\n"
   "int 

[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-18 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 401062.
ksyx marked 2 inline comments as done.
ksyx edited the summary of this revision.
ksyx added a comment.

- Resolves formatting conflict with options `EmptyLineAfterAccessModifier` and 
`EmptyLineBeforeAccessModifier` (prompts with `--dry-run` (`-n`) or 
`--output-replacement-xml` but no observable change)
- Remove redundant and repeated initialization
- Do no change to newlines before EOF


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -130,6 +130,41 @@
Style);
 }
 
+TEST_F(DefinitionBlockSeparatorTest, CommentBlock) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  std::string Prefix = "enum Foo { FOO, BAR };\n"
+   "\n"
+   "/*\n"
+   "test1\n"
+   "test2\n"
+   "*/\n"
+   "int foo(int i, int j) {\n"
+   "  int r = i + j;\n"
+   "  return r;\n"
+   "}\n";
+  std::string Suffix = "enum Bar { FOOBAR, BARFOO };\n"
+   "\n"
+   "/* Comment block in one line*/\n"
+   "int bar3(int j, int k) {\n"
+   "  // A comment\n"
+   "  int r = j % k;\n"
+   "  return r;\n"
+   "}\n";
+  std::string CommentedCode = "/*\n"
+  "int bar2(int j, int k) {\n"
+  "  int r = j / k;\n"
+  "  return r;\n"
+  "}\n"
+  "*/\n";
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode + "\n" +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + "\n" + Suffix);
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + Suffix);
+}
+
 TEST_F(DefinitionBlockSeparatorTest, UntouchBlockStartStyle) {
   // Returns a std::pair of two strings, with the first one for passing into
   // Always test and the second one be the expected result of the first string.
@@ -172,13 +207,15 @@
   FormatStyle NeverStyle = getLLVMStyle();
   NeverStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
 
-  auto TestKit = MakeUntouchTest("#ifdef FOO\n\n", "\n#elifndef BAR\n\n",
- "\n#endif\n\n", false);
+  auto TestKit = MakeUntouchTest("/* FOOBAR */\n"
+ "#ifdef FOO\n\n",
+ "\n#elifndef BAR\n\n", "\n#endif\n\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
-  TestKit =
-  MakeUntouchTest("#ifdef FOO\n", "#elifndef BAR\n", "#endif\n", false);
+  TestKit = MakeUntouchTest("/* FOOBAR */\n"
+"#ifdef FOO\n",
+"#elifndef BAR\n", "#endif\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
@@ -210,7 +247,7 @@
   "test1\n"
   "test2\n"
   "*/\n"
-  "int foo(int i, int j) {\n"
+  "/*const*/ int foo(int i, int j) {\n"
   "  int r = i + j;\n"
   "  return r;\n"
   "}\n"
@@ -222,8 +259,10 @@
   "// Comment line 2\n"
   "// Comment line 3\n"
   "int bar(int j, int k) {\n"
-  "  int r = j * k;\n"
-  "  return r;\n"
+  "  {\n"
+  "int r = j * k;\n"
+  "return r;\n"
+  "  }\n"
   "}\n"
   "\n"
   "int bar2(int j, int k) {\n"
@@ -234,7 +273,7 @@
   "/* Comment block in one line*/\n"
   "enum Bar { FOOBAR, BARFOO };\n"
   "\n"
-  "int bar3(int j, int k) {\n"
+  "int bar3(int j, int k, const enum Bar b) {\n"
   "  // A comment\n"
   "  int r = j % k;\n"
   "  return r;\n"
@@ 

[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-18 Thread ksyx via Phabricator via cfe-commits
ksyx added inline comments.



Comment at: clang/lib/Format/DefinitionBlockSeparator.cpp:80
 return;
   Whitespaces.replaceWhitespace(*TargetToken, NewlineToInsert,
+TargetToken->OriginalColumn,

HazardyKnusperkeks wrote:
> What if NewlineToInsert is 0? Does that explain @MyDeveloperDay s observation?
Then the line break would be removed (concatenate with last line), which is not 
supposed to happen with Always style but possible at first line of Never style. 

I have located the problem and roughly the solution but it takes some time to 
find a neat fix :)

Thanks for the feedback


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

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


[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-18 Thread ksyx via Phabricator via cfe-commits
ksyx added a comment.

In D117520#3251425 , @MyDeveloperDay 
wrote:

> For
>
>   class Test
>   {
>   public:
>   static void foo()
>   {
>   }
>   };
>
> F21703157: image.png 
>
>   $ clang-format -n test.cxx
>   test.cxx:3:8: warning: code should be clang-formatted 
> [-Wclang-format-violations
>   ]
>   public:
>  ^
>
>
>
>   $ clang-format --output-replacements-xml test.cxx
>   
>   
>   
>   
>
> but when I run it nothing changes
>
>   $ clang-format test.cxx
>   class Test
>   {
>   public:
>   static void foo()
>   {
>   }
>   };
>
> And no character change
>
> F21703226: image.png 

Looks like it is just replacing indent whitespaces to tabs


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

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


[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-18 Thread ksyx via Phabricator via cfe-commits
ksyx added a comment.

In D117520#3251376 , @MyDeveloperDay 
wrote:

> I'm getting some sort of seemingly empty replacements going on, after 
> `public:` I can't quite put my finger on what's happening.

Not so sure what empty means here. Is it removing empty line following 
`public:` or adding, or something else?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

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


[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-18 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 400821.
ksyx marked 3 inline comments as done.
ksyx edited the summary of this revision.
ksyx added a comment.

- Use function keyword for JavaScript instead of comparing strings
- Apply review suggestions


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -130,6 +130,41 @@
Style);
 }
 
+TEST_F(DefinitionBlockSeparatorTest, CommentBlock) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  std::string Prefix = "enum Foo { FOO, BAR };\n"
+   "\n"
+   "/*\n"
+   "test1\n"
+   "test2\n"
+   "*/\n"
+   "int foo(int i, int j) {\n"
+   "  int r = i + j;\n"
+   "  return r;\n"
+   "}\n";
+  std::string Suffix = "enum Bar { FOOBAR, BARFOO };\n"
+   "\n"
+   "/* Comment block in one line*/\n"
+   "int bar3(int j, int k) {\n"
+   "  // A comment\n"
+   "  int r = j % k;\n"
+   "  return r;\n"
+   "}\n";
+  std::string CommentedCode = "/*\n"
+  "int bar2(int j, int k) {\n"
+  "  int r = j / k;\n"
+  "  return r;\n"
+  "}\n"
+  "*/\n";
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode + "\n" +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + "\n" + Suffix);
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + Suffix);
+}
+
 TEST_F(DefinitionBlockSeparatorTest, UntouchBlockStartStyle) {
   // Returns a std::pair of two strings, with the first one for passing into
   // Always test and the second one be the expected result of the first string.
@@ -172,13 +207,15 @@
   FormatStyle NeverStyle = getLLVMStyle();
   NeverStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
 
-  auto TestKit = MakeUntouchTest("#ifdef FOO\n\n", "\n#elifndef BAR\n\n",
- "\n#endif\n\n", false);
+  auto TestKit = MakeUntouchTest("/* FOOBAR */\n"
+ "#ifdef FOO\n\n",
+ "\n#elifndef BAR\n\n", "\n#endif\n\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
-  TestKit =
-  MakeUntouchTest("#ifdef FOO\n", "#elifndef BAR\n", "#endif\n", false);
+  TestKit = MakeUntouchTest("/* FOOBAR */\n"
+"#ifdef FOO\n",
+"#elifndef BAR\n", "#endif\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
@@ -210,7 +247,7 @@
   "test1\n"
   "test2\n"
   "*/\n"
-  "int foo(int i, int j) {\n"
+  "/*const*/ int foo(int i, int j) {\n"
   "  int r = i + j;\n"
   "  return r;\n"
   "}\n"
@@ -222,8 +259,10 @@
   "// Comment line 2\n"
   "// Comment line 3\n"
   "int bar(int j, int k) {\n"
-  "  int r = j * k;\n"
-  "  return r;\n"
+  "  {\n"
+  "int r = j * k;\n"
+  "return r;\n"
+  "  }\n"
   "}\n"
   "\n"
   "int bar2(int j, int k) {\n"
@@ -234,7 +273,7 @@
   "/* Comment block in one line*/\n"
   "enum Bar { FOOBAR, BARFOO };\n"
   "\n"
-  "int bar3(int j, int k) {\n"
+  "int bar3(int j, int k, const enum Bar b) {\n"
   "  // A comment\n"
   "  int r = j % k;\n"
   "  return r;\n"
@@ -261,7 +300,7 @@
 "test1\n"
 "test2\n"
 "*/\n"
-"int foo(int i, int j) {\n"
+

[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-17 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 400667.
ksyx added a comment.

Apply clangfmt's suggestion.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117520/new/

https://reviews.llvm.org/D117520

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -130,6 +130,41 @@
Style);
 }
 
+TEST_F(DefinitionBlockSeparatorTest, CommentBlock) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  std::string Prefix = "enum Foo { FOO, BAR };\n"
+   "\n"
+   "/*\n"
+   "test1\n"
+   "test2\n"
+   "*/\n"
+   "int foo(int i, int j) {\n"
+   "  int r = i + j;\n"
+   "  return r;\n"
+   "}\n";
+  std::string Suffix = "enum Bar { FOOBAR, BARFOO };\n"
+   "\n"
+   "/* Comment block in one line*/\n"
+   "int bar3(int j, int k) {\n"
+   "  // A comment\n"
+   "  int r = j % k;\n"
+   "  return r;\n"
+   "}\n";
+  std::string CommentedCode = "/*\n"
+  "int bar2(int j, int k) {\n"
+  "  int r = j / k;\n"
+  "  return r;\n"
+  "}\n"
+  "*/\n";
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode + "\n" +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + "\n" + Suffix);
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + Suffix);
+}
+
 TEST_F(DefinitionBlockSeparatorTest, UntouchBlockStartStyle) {
   // Returns a std::pair of two strings, with the first one for passing into
   // Always test and the second one be the expected result of the first string.
@@ -172,13 +207,15 @@
   FormatStyle NeverStyle = getLLVMStyle();
   NeverStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
 
-  auto TestKit = MakeUntouchTest("#ifdef FOO\n\n", "\n#elifndef BAR\n\n",
- "\n#endif\n\n", false);
+  auto TestKit = MakeUntouchTest("/* FOOBAR */\n"
+ "#ifdef FOO\n\n",
+ "\n#elifndef BAR\n\n", "\n#endif\n\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
-  TestKit =
-  MakeUntouchTest("#ifdef FOO\n", "#elifndef BAR\n", "#endif\n", false);
+  TestKit = MakeUntouchTest("/* FOOBAR */\n"
+"#ifdef FOO\n",
+"#elifndef BAR\n", "#endif\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
@@ -210,7 +247,7 @@
   "test1\n"
   "test2\n"
   "*/\n"
-  "int foo(int i, int j) {\n"
+  "/*const*/ int foo(int i, int j) {\n"
   "  int r = i + j;\n"
   "  return r;\n"
   "}\n"
@@ -222,8 +259,10 @@
   "// Comment line 2\n"
   "// Comment line 3\n"
   "int bar(int j, int k) {\n"
-  "  int r = j * k;\n"
-  "  return r;\n"
+  "  {\n"
+  "int r = j * k;\n"
+  "return r;\n"
+  "  }\n"
   "}\n"
   "\n"
   "int bar2(int j, int k) {\n"
@@ -234,7 +273,7 @@
   "/* Comment block in one line*/\n"
   "enum Bar { FOOBAR, BARFOO };\n"
   "\n"
-  "int bar3(int j, int k) {\n"
+  "int bar3(int j, int k, const enum Bar b) {\n"
   "  // A comment\n"
   "  int r = j % k;\n"
   "  return r;\n"
@@ -261,7 +300,7 @@
 "test1\n"
 "test2\n"
 "*/\n"
-"int foo(int i, int j) {\n"
+"/*const*/ int foo(int i, int j) {\n"
 "  int r = i + j;\n"
 "  return r;\n"
 "}\n"
@@ -273,8 +312,10 @@
 

[PATCH] D117520: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-17 Thread ksyx via Phabricator via cfe-commits
ksyx created this revision.
ksyx added reviewers: MyDeveloperDay, curdeius, HazardyKnusperkeks.
ksyx requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

- Fixes https://github.com/llvm/llvm-project/issues/53227 that wrongly indents 
multiline comments
- Fixes wrong detection of single-line opening bracket when used along with 
those only opening scopes:

void foo()
{
  {
int x;
  }
}

- Fixes wrong recognition of first line of definition when the line starts with 
block comment:

  /*
 Some descriptions about function
  */
/*inline*/ void bar() {
}

- Fixes wrong recognition of enum when used as a type name rather than starting 
definition block:

void foobar(const enum EnumType e) {
}


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117520

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -130,6 +130,41 @@
Style);
 }
 
+TEST_F(DefinitionBlockSeparatorTest, CommentBlock) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  std::string Prefix = "enum Foo { FOO, BAR };\n"
+   "\n"
+   "/*\n"
+   "test1\n"
+   "test2\n"
+   "*/\n"
+   "int foo(int i, int j) {\n"
+   "  int r = i + j;\n"
+   "  return r;\n"
+   "}\n";
+  std::string Suffix = "enum Bar { FOOBAR, BARFOO };\n"
+"\n"
+"/* Comment block in one line*/\n"
+"int bar3(int j, int k) {\n"
+"  // A comment\n"
+"  int r = j % k;\n"
+"  return r;\n"
+"}\n";
+  std::string CommentedCode = "/*\n"
+  "int bar2(int j, int k) {\n"
+  "  int r = j / k;\n"
+  "  return r;\n"
+  "}\n"
+  "*/\n";
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode + "\n" +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + "\n" + Suffix);
+  verifyFormat(removeEmptyLines(Prefix) + "\n" + CommentedCode +
+   removeEmptyLines(Suffix),
+   Style, Prefix + "\n" + CommentedCode + Suffix);
+}
+
 TEST_F(DefinitionBlockSeparatorTest, UntouchBlockStartStyle) {
   // Returns a std::pair of two strings, with the first one for passing into
   // Always test and the second one be the expected result of the first string.
@@ -172,13 +207,17 @@
   FormatStyle NeverStyle = getLLVMStyle();
   NeverStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
 
-  auto TestKit = MakeUntouchTest("#ifdef FOO\n\n", "\n#elifndef BAR\n\n",
+  auto TestKit = MakeUntouchTest("/* FOOBAR */\n"
+ "#ifdef FOO\n\n",
+ "\n#elifndef BAR\n\n",
  "\n#endif\n\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
-  TestKit =
-  MakeUntouchTest("#ifdef FOO\n", "#elifndef BAR\n", "#endif\n", false);
+  TestKit = MakeUntouchTest("/* FOOBAR */\n"
+"#ifdef FOO\n",
+"#elifndef BAR\n",
+"#endif\n", false);
   verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
   verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
 
@@ -210,7 +249,7 @@
   "test1\n"
   "test2\n"
   "*/\n"
-  "int foo(int i, int j) {\n"
+  "/*const*/ int foo(int i, int j) {\n"
   "  int r = i + j;\n"
   "  return r;\n"
   "}\n"
@@ -222,8 +261,10 @@
   "// Comment line 2\n"
   "// Comment line 3\n"
   "int bar(int j, int k) {\n"
-  "  int r = j * k;\n"
-  "  return r;\n"
+  "  {\n"
+  "int r = j * k;\n"
+  "return r;\n"
+  "  }\n"
   "}\n"
   "\n"
   "int bar2(int j, int k) {\n"
@@ -234,7 +275,7 @@
   "/* 

[PATCH] D92257: [clang-format] Add option to control the space at the front of a line comment

2022-01-17 Thread ksyx via Phabricator via cfe-commits
ksyx added a comment.

In D92257#3004563 , 
@HazardyKnusperkeks wrote:

> In D92257#3003281 , @byronhe wrote:
>
>> Hi guys,i found `SpacesInLineCommentPrefix` does not support other encoding 
>> such as utf8 ,
>> I am curious why there is a `isAlphanumeric` limit in 
>> `BreakableLineCommentSection::BreakableLineCommentSection()` ?
>> I want to make some contribution to make it support utf8, what should i do ?
>
> The `isAlphanumeric` is there to not break doxygen like comments for example.
>
> I'm very interested in how you want to tackle that problem. :)

Sorry for digging out this month long thing but I encountered this in one 
project full of non-alphanumeric comment. If I understood the problem 
correctly, is it more like avoiding symbols like @ / * rather than avoiding 
non-ASCII characters like CJK's? In this case, would just not adding space when 
it is started with symbols make this option usable for wider amount of comment 
content? :)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D92257/new/

https://reviews.llvm.org/D92257

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


[PATCH] D116663: [clang-format] Fix SeparateDefinitionBlocks issues

2022-01-11 Thread ksyx via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGee25a327aac0: [clang-format] Fix SeparateDefinitionBlocks 
issues (authored by ksyx).
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116663/new/

https://reviews.llvm.org/D116663

Files:
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -57,8 +57,9 @@
   InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
 EXPECT_EQ(ExpectedCode.str(), separateDefinitionBlocks(ExpectedCode, Style))
 << "Expected code is not stable";
-std::string InverseResult = separateDefinitionBlocks(Code, InverseStyle);
-EXPECT_NE(Code.str(), InverseResult)
+std::string InverseResult =
+separateDefinitionBlocks(ExpectedCode, InverseStyle);
+EXPECT_NE(ExpectedCode.str(), InverseResult)
 << "Inverse formatting makes no difference";
 std::string CodeToFormat =
 HasOriginalCode ? Code.str() : removeEmptyLines(Code);
@@ -129,49 +130,166 @@
Style);
 }
 
+TEST_F(DefinitionBlockSeparatorTest, UntouchBlockStartStyle) {
+  // Returns a std::pair of two strings, with the first one for passing into
+  // Always test and the second one be the expected result of the first string.
+  auto MakeUntouchTest = [&](std::string BlockHeader, std::string BlockChanger,
+ std::string BlockFooter, bool BlockEndNewLine) {
+std::string CodePart1 = "enum Foo { FOO, BAR };\n"
+"\n"
+"/*\n"
+"test1\n"
+"test2\n"
+"*/\n"
+"int foo(int i, int j) {\n"
+"  int r = i + j;\n"
+"  return r;\n"
+"}\n";
+std::string CodePart2 = "/* Comment block in one line*/\n"
+"enum Bar { FOOBAR, BARFOO };\n"
+"\n"
+"int bar3(int j, int k) {\n"
+"  // A comment\n"
+"  int r = j % k;\n"
+"  return r;\n"
+"}\n";
+std::string CodePart3 = "int bar2(int j, int k) {\n"
+"  int r = j / k;\n"
+"  return r;\n"
+"}\n";
+std::string ConcatAll = BlockHeader + CodePart1 + BlockChanger + CodePart2 +
+BlockFooter + (BlockEndNewLine ? "\n" : "") +
+CodePart3;
+return std::make_pair(BlockHeader + removeEmptyLines(CodePart1) +
+  BlockChanger + removeEmptyLines(CodePart2) +
+  BlockFooter + removeEmptyLines(CodePart3),
+  ConcatAll);
+  };
+
+  FormatStyle AlwaysStyle = getLLVMStyle();
+  AlwaysStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+
+  FormatStyle NeverStyle = getLLVMStyle();
+  NeverStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
+
+  auto TestKit = MakeUntouchTest("#ifdef FOO\n\n", "\n#elifndef BAR\n\n",
+ "\n#endif\n\n", false);
+  verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
+  verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
+
+  TestKit =
+  MakeUntouchTest("#ifdef FOO\n", "#elifndef BAR\n", "#endif\n", false);
+  verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
+  verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
+
+  TestKit = MakeUntouchTest("namespace Ns {\n\n",
+"\n} // namespace Ns\n\n"
+"namespace {\n\n",
+"\n} // namespace\n", true);
+  verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
+  verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
+
+  TestKit = MakeUntouchTest("namespace Ns {\n",
+"} // namespace Ns\n\n"
+"namespace {\n",
+"} // namespace\n", true);
+  verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
+  verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
+}
+
 TEST_F(DefinitionBlockSeparatorTest, Always) {
   FormatStyle Style = getLLVMStyle();
   Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
   std::string Prefix = "namespace {\n";
-  std::string Postfix = "enum Foo { FOO, BAR };\n"
-   

[PATCH] D116314: [clang-format] Add style to separate definition blocks

2022-01-05 Thread ksyx via Phabricator via cfe-commits
ksyx added a comment.

In D116314#3221488 , @MyDeveloperDay 
wrote:

> @ksyx
>
> Did you see this issue https://github.com/llvm/llvm-project/issues/52976
>
> In its current form, this patch causes huge amounts of flux in projects
> that document code like (anyone using doxygen likely, + most normal people
> ;-) )
>
> ...
> }
>
> // My function
>   extra newline added
> void
> foo()
> {
> }
>
> as it will place and undesirable additional newline between the function
> and the return type.
>
> We really need this to be resolved before we branch out v14 (which may be
> imminent)
>
> MyDeveloperDay

Thanks for the feedback! I have proposed some fix in D116663 
, and please see if this works.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

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


[PATCH] D116557: [clang-format] Fix SeparateDefinitionBlocks docs and ...

2022-01-03 Thread ksyx via Phabricator via cfe-commits
ksyx accepted this revision.
ksyx added a comment.
This revision is now accepted and ready to land.

Thanks for the fix!


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116557/new/

https://reviews.llvm.org/D116557

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


[PATCH] D116557: [clang-format] Fix SeparateDefinitionBlocks docs and ...

2022-01-03 Thread ksyx via Phabricator via cfe-commits
ksyx added a comment.

Thanks for the fix :)




Comment at: clang/docs/ReleaseNotes.rst:300
 
 - Option ``SeparateDefinitionBlocks`` has been added to insert or remove empty
   lines between definition blocks including functions, classes, structs, enums,

The change is documented here to group with added options?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116557/new/

https://reviews.llvm.org/D116557

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


[PATCH] D116314: [clang-format] Add style to separate definition blocks

2022-01-03 Thread ksyx via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG6f6f88ffdae1: [clang-format] Style to separate definition 
blocks (authored by ksyx).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/lib/Format/WhitespaceManager.h
  clang/unittests/Format/CMakeLists.txt
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- /dev/null
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -0,0 +1,309 @@
+//===- DefinitionBlockSeparatorTest.cpp - Formatting unit tests ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "FormatTestUtils.h"
+#include "clang/Format/Format.h"
+
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "definition-block-separator-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class DefinitionBlockSeparatorTest : public ::testing::Test {
+protected:
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const std::vector ,
+   const FormatStyle  = getLLVMStyle()) {
+LLVM_DEBUG(llvm::errs() << "---\n");
+LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+tooling::Replacements Replaces = reformat(Style, Code, Ranges, "");
+auto Result = applyAllReplacements(Code, Replaces);
+EXPECT_TRUE(static_cast(Result));
+LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+return *Result;
+  }
+
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle()) {
+return separateDefinitionBlocks(
+Code,
+/*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
+  }
+
+  static void verifyFormat(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle(),
+   llvm::StringRef ExpectedCode = "") {
+bool HasOriginalCode = true;
+if (ExpectedCode == "") {
+  ExpectedCode = Code;
+  HasOriginalCode = false;
+}
+
+FormatStyle InverseStyle = Style;
+if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always)
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
+else
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+EXPECT_EQ(ExpectedCode.str(), separateDefinitionBlocks(ExpectedCode, Style))
+<< "Expected code is not stable";
+std::string InverseResult = separateDefinitionBlocks(Code, InverseStyle);
+EXPECT_NE(Code.str(), InverseResult)
+<< "Inverse formatting makes no difference";
+std::string CodeToFormat =
+HasOriginalCode ? Code.str() : removeEmptyLines(Code);
+std::string Result = separateDefinitionBlocks(CodeToFormat, Style);
+EXPECT_EQ(ExpectedCode.str(), Result) << "Test failed. Formatted:\n"
+  << Result;
+  }
+
+  static std::string removeEmptyLines(llvm::StringRef Code) {
+std::string Result = "";
+for (auto Char : Code.str()) {
+  if (Result.size()) {
+auto LastChar = Result.back();
+if ((Char == '\n' && LastChar == '\n') ||
+(Char == '\r' && (LastChar == '\r' || LastChar == '\n')))
+  continue;
+  }
+  Result.push_back(Char);
+}
+return Result;
+  }
+};
+
+TEST_F(DefinitionBlockSeparatorTest, Basic) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  verifyFormat("int foo(int i, int j) {\n"
+   "  int r = i + j;\n"
+   "  return r;\n"
+   "}\n"
+   "\n"
+   "int bar(int j, int k) {\n"
+   "  int r = j + k;\n"
+   "  return r;\n"
+   "}",
+   Style);
+
+  verifyFormat("struct foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "struct bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("class foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "class bar {\n"
+   "  int j, k;\n"
+

[PATCH] D116314: [clang-format] Add style to separate definition blocks

2022-01-02 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396926.
ksyx added a comment.

Add release note entry.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/lib/Format/WhitespaceManager.h
  clang/unittests/Format/CMakeLists.txt
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- /dev/null
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -0,0 +1,309 @@
+//===- DefinitionBlockSeparatorTest.cpp - Formatting unit tests ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "FormatTestUtils.h"
+#include "clang/Format/Format.h"
+
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "definition-block-separator-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class DefinitionBlockSeparatorTest : public ::testing::Test {
+protected:
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const std::vector ,
+   const FormatStyle  = getLLVMStyle()) {
+LLVM_DEBUG(llvm::errs() << "---\n");
+LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+tooling::Replacements Replaces = reformat(Style, Code, Ranges, "");
+auto Result = applyAllReplacements(Code, Replaces);
+EXPECT_TRUE(static_cast(Result));
+LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+return *Result;
+  }
+
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle()) {
+return separateDefinitionBlocks(
+Code,
+/*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
+  }
+
+  static void verifyFormat(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle(),
+   llvm::StringRef ExpectedCode = "") {
+bool HasOriginalCode = true;
+if (ExpectedCode == "") {
+  ExpectedCode = Code;
+  HasOriginalCode = false;
+}
+
+FormatStyle InverseStyle = Style;
+if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always)
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
+else
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+EXPECT_EQ(ExpectedCode.str(), separateDefinitionBlocks(ExpectedCode, Style))
+<< "Expected code is not stable";
+std::string InverseResult = separateDefinitionBlocks(Code, InverseStyle);
+EXPECT_NE(Code.str(), InverseResult)
+<< "Inverse formatting makes no difference";
+std::string CodeToFormat =
+HasOriginalCode ? Code.str() : removeEmptyLines(Code);
+std::string Result = separateDefinitionBlocks(CodeToFormat, Style);
+EXPECT_EQ(ExpectedCode.str(), Result) << "Test failed. Formatted:\n"
+  << Result;
+  }
+
+  static std::string removeEmptyLines(llvm::StringRef Code) {
+std::string Result = "";
+for (auto Char : Code.str()) {
+  if (Result.size()) {
+auto LastChar = Result.back();
+if ((Char == '\n' && LastChar == '\n') ||
+(Char == '\r' && (LastChar == '\r' || LastChar == '\n')))
+  continue;
+  }
+  Result.push_back(Char);
+}
+return Result;
+  }
+};
+
+TEST_F(DefinitionBlockSeparatorTest, Basic) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  verifyFormat("int foo(int i, int j) {\n"
+   "  int r = i + j;\n"
+   "  return r;\n"
+   "}\n"
+   "\n"
+   "int bar(int j, int k) {\n"
+   "  int r = j + k;\n"
+   "  return r;\n"
+   "}",
+   Style);
+
+  verifyFormat("struct foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "struct bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("class foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "class bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("namespace foo {\n"
+   "int i, j;\n"
+   "}\n"
+   "\n"
+   "namespace bar 

[PATCH] D116314: [clang-format] Add style to separate definition blocks

2022-01-01 Thread ksyx via Phabricator via cfe-commits
ksyx added inline comments.



Comment at: clang/docs/ClangFormatStyleOptions.rst:3398
 
+**SeparateDefinitionBlocks** (``SeparateDefinitionStyle``) 
:versionbadge:`clang-format 14`
+  Specifies the use of empty lines to separate definition blocks, including 
classes,

MyDeveloperDay wrote:
> EmptyLinesBetweenBlocks?
It feel like for me that it is implying that there will also be empty lines 
surrounding blocks of, for example, `for` and `if`?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

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


[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-31 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396835.
ksyx marked 3 inline comments as done.
ksyx added a comment.

Change the `SDS_Leave` style check to be assert not having this style.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/lib/Format/WhitespaceManager.h
  clang/unittests/Format/CMakeLists.txt
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- /dev/null
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -0,0 +1,309 @@
+//===- DefinitionBlockSeparatorTest.cpp - Formatting unit tests ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "FormatTestUtils.h"
+#include "clang/Format/Format.h"
+
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "definition-block-separator-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class DefinitionBlockSeparatorTest : public ::testing::Test {
+protected:
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const std::vector ,
+   const FormatStyle  = getLLVMStyle()) {
+LLVM_DEBUG(llvm::errs() << "---\n");
+LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+tooling::Replacements Replaces = reformat(Style, Code, Ranges, "");
+auto Result = applyAllReplacements(Code, Replaces);
+EXPECT_TRUE(static_cast(Result));
+LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+return *Result;
+  }
+
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle()) {
+return separateDefinitionBlocks(
+Code,
+/*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
+  }
+
+  static void verifyFormat(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle(),
+   llvm::StringRef ExpectedCode = "") {
+bool HasOriginalCode = true;
+if (ExpectedCode == "") {
+  ExpectedCode = Code;
+  HasOriginalCode = false;
+}
+
+FormatStyle InverseStyle = Style;
+if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always)
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
+else
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+EXPECT_EQ(ExpectedCode.str(), separateDefinitionBlocks(ExpectedCode, Style))
+<< "Expected code is not stable";
+std::string InverseResult = separateDefinitionBlocks(Code, InverseStyle);
+EXPECT_NE(Code.str(), InverseResult)
+<< "Inverse formatting makes no difference";
+std::string CodeToFormat =
+HasOriginalCode ? Code.str() : removeEmptyLines(Code);
+std::string Result = separateDefinitionBlocks(CodeToFormat, Style);
+EXPECT_EQ(ExpectedCode.str(), Result) << "Test failed. Formatted:\n"
+  << Result;
+  }
+
+  static std::string removeEmptyLines(llvm::StringRef Code) {
+std::string Result = "";
+for (auto Char : Code.str()) {
+  if (Result.size()) {
+auto LastChar = Result.back();
+if ((Char == '\n' && LastChar == '\n') ||
+(Char == '\r' && (LastChar == '\r' || LastChar == '\n')))
+  continue;
+  }
+  Result.push_back(Char);
+}
+return Result;
+  }
+};
+
+TEST_F(DefinitionBlockSeparatorTest, Basic) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  verifyFormat("int foo(int i, int j) {\n"
+   "  int r = i + j;\n"
+   "  return r;\n"
+   "}\n"
+   "\n"
+   "int bar(int j, int k) {\n"
+   "  int r = j + k;\n"
+   "  return r;\n"
+   "}",
+   Style);
+
+  verifyFormat("struct foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "struct bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("class foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "class bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("namespace foo {\n"
+   "int i, j;\n"
+   

[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-31 Thread ksyx via Phabricator via cfe-commits
ksyx marked an inline comment as done.
ksyx added inline comments.



Comment at: clang/lib/Format/DefinitionBlockSeparator.cpp:25
+FormatTokenLexer ) {
+  if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Leave)
+return {{}, 0};

HazardyKnusperkeks wrote:
> ksyx wrote:
> > HazardyKnusperkeks wrote:
> > > Better, but I still think we should have the assert here. The class 
> > > should not be instantiated at all, if you don't want to use it.
> > My idea is that it would be too redundant to have every user of this method 
> > to check style once before it calls it?
> Who are the users? `clang-format` and maybe its tests. And the only one that 
> really matters is `clang-format`, which does the check.
Does there exist the possibility that some developer just include the header 
and link to library to obtain replacement analysis result for their other parts 
of program to use, instead of only clangfmt itself is using this class?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

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


[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-31 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396826.
ksyx added a comment.

Use `reformat` method instead of calling the single method only.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/lib/Format/WhitespaceManager.h
  clang/unittests/Format/CMakeLists.txt
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- /dev/null
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -0,0 +1,309 @@
+//===- DefinitionBlockSeparatorTest.cpp - Formatting unit tests ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "FormatTestUtils.h"
+#include "clang/Format/Format.h"
+
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "definition-block-separator-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class DefinitionBlockSeparatorTest : public ::testing::Test {
+protected:
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const std::vector ,
+   const FormatStyle  = getLLVMStyle()) {
+LLVM_DEBUG(llvm::errs() << "---\n");
+LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+tooling::Replacements Replaces = reformat(Style, Code, Ranges, "");
+auto Result = applyAllReplacements(Code, Replaces);
+EXPECT_TRUE(static_cast(Result));
+LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+return *Result;
+  }
+
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle()) {
+return separateDefinitionBlocks(
+Code,
+/*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
+  }
+
+  static void verifyFormat(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle(),
+   llvm::StringRef ExpectedCode = "") {
+bool HasOriginalCode = true;
+if (ExpectedCode == "") {
+  ExpectedCode = Code;
+  HasOriginalCode = false;
+}
+
+FormatStyle InverseStyle = Style;
+if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always)
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
+else
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+EXPECT_EQ(ExpectedCode.str(), separateDefinitionBlocks(ExpectedCode, Style))
+<< "Expected code is not stable";
+std::string InverseResult = separateDefinitionBlocks(Code, InverseStyle);
+EXPECT_NE(Code.str(), InverseResult)
+<< "Inverse formatting makes no difference";
+std::string CodeToFormat =
+HasOriginalCode ? Code.str() : removeEmptyLines(Code);
+std::string Result = separateDefinitionBlocks(CodeToFormat, Style);
+EXPECT_EQ(ExpectedCode.str(), Result) << "Test failed. Formatted:\n"
+  << Result;
+  }
+
+  static std::string removeEmptyLines(llvm::StringRef Code) {
+std::string Result = "";
+for (auto Char : Code.str()) {
+  if (Result.size()) {
+auto LastChar = Result.back();
+if ((Char == '\n' && LastChar == '\n') ||
+(Char == '\r' && (LastChar == '\r' || LastChar == '\n')))
+  continue;
+  }
+  Result.push_back(Char);
+}
+return Result;
+  }
+};
+
+TEST_F(DefinitionBlockSeparatorTest, Basic) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  verifyFormat("int foo(int i, int j) {\n"
+   "  int r = i + j;\n"
+   "  return r;\n"
+   "}\n"
+   "\n"
+   "int bar(int j, int k) {\n"
+   "  int r = j + k;\n"
+   "  return r;\n"
+   "}",
+   Style);
+
+  verifyFormat("struct foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "struct bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("class foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "class bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("namespace foo {\n"
+   "int i, j;\n"
+   "}\n"
+   "\n"
+   

[PATCH] D116351: Update Bug report URL to Github Issues

2021-12-31 Thread ksyx via Phabricator via cfe-commits
ksyx added inline comments.



Comment at: libcxx/docs/index.rst:220
 * `libc++abi Homepage `_
-* `LLVM Bugzilla `_
+* `LLVM Issues `_
 * `libcxx-commits Mailing List`_

MaskRay wrote:
> Quuxplusone wrote:
> > 
> Would https://github.com/llvm/llvm-project/labels/libc++ be better?
> 
> Unfortunately "New issue" on that page does not apply the label automatically.
Adding parameter like in 
 could be the most 
direct way of doing this, given the label exists, so do assignee (untested)? 
Besides, the new 
[[ 
https://docs.github.com/en/communities/using-templates-to-encourage-useful-issues-and-pull-requests/syntax-for-issue-forms
 | Issue Forms ]] feature of GitHub is also a good idea that also unifys what 
to fill out in a request and what to automatically do with the issue.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116351/new/

https://reviews.llvm.org/D116351

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


[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-31 Thread ksyx via Phabricator via cfe-commits
ksyx added inline comments.



Comment at: clang/lib/Format/DefinitionBlockSeparator.cpp:25
+FormatTokenLexer ) {
+  if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Leave)
+return {{}, 0};

HazardyKnusperkeks wrote:
> Better, but I still think we should have the assert here. The class should 
> not be instantiated at all, if you don't want to use it.
My idea is that it would be too redundant to have every user of this method to 
check style once before it calls it?



Comment at: clang/lib/Format/DefinitionBlockSeparator.cpp:35
+return;
+  auto likelyDefinition = [this](AnnotatedLine *Line) {
+if (Line->MightBeFunctionDecl && Line->mightBeFunctionDefinition())

HazardyKnusperkeks wrote:
> ksyx wrote:
> > HazardyKnusperkeks wrote:
> > > 
> > [[ 
> > https://github.com/llvm/llvm-project/blob/298367ee6e36eeb1b193ad9fa92082c2ef2345a3/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp#L2393
> >  | Some other lambdas ]] I found use lowercased leading letter, as it looks 
> > like a function (call).
> Okay, then I don't know what the LLVM Style is, probably there is none in 
> this regard. I think the lambdas I saw in clang-format are with a capital 
> letter.
Ok. Then I will make changes.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

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


[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-31 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396782.
ksyx added a comment.

Change lambda variable names' leading letter to uppercase.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/lib/Format/WhitespaceManager.h
  clang/unittests/Format/CMakeLists.txt
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- /dev/null
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -0,0 +1,316 @@
+//===- DefinitionBlockSeparatorTest.cpp - Formatting unit tests ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "FormatTestUtils.h"
+#include "clang/Format/Format.h"
+
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "definition-block-separator-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class DefinitionBlockSeparatorTest : public ::testing::Test {
+protected:
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const std::vector ,
+   const FormatStyle  = getLLVMStyle()) {
+LLVM_DEBUG(llvm::errs() << "---\n");
+LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+tooling::Replacements Replaces =
+clang::format::separateDefinitionBlocks(Style, Code, Ranges, "");
+auto Result = applyAllReplacements(Code, Replaces);
+EXPECT_TRUE(static_cast(Result));
+LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+return *Result;
+  }
+
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle()) {
+return separateDefinitionBlocks(
+Code,
+/*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
+  }
+
+  static void verifyFormat(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle(),
+   llvm::StringRef ExpectedCode = "") {
+bool HasOriginalCode = true;
+if (ExpectedCode == "") {
+  ExpectedCode = Code;
+  HasOriginalCode = false;
+}
+
+FormatStyle InverseStyle = Style;
+if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always)
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
+else
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+EXPECT_EQ(ExpectedCode.str(), separateDefinitionBlocks(ExpectedCode, Style))
+<< "Expected code is not stable";
+std::string InverseResult = separateDefinitionBlocks(Code, InverseStyle);
+EXPECT_NE(Code.str(), InverseResult)
+<< "Inverse formatting makes no difference";
+std::string CodeToFormat =
+HasOriginalCode ? Code.str() : removeEmptyLines(Code);
+std::string Result = separateDefinitionBlocks(CodeToFormat, Style);
+EXPECT_EQ(ExpectedCode.str(), Result) << "Test failed. Formatted:\n"
+  << Result;
+  }
+
+  static std::string removeEmptyLines(llvm::StringRef Code) {
+std::string Result = "";
+for (auto Char : Code.str()) {
+  if (Result.size()) {
+auto LastChar = Result.back();
+if ((Char == '\n' && LastChar == '\n') ||
+(Char == '\r' && (LastChar == '\r' || LastChar == '\n')))
+  continue;
+  }
+  Result.push_back(Char);
+}
+return Result;
+  }
+};
+
+TEST_F(DefinitionBlockSeparatorTest, Basic) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  verifyFormat("int foo() {\n"
+   "  int i, j;\n"
+   "}\n"
+   "\n"
+   "int bar() {\n"
+   "  int j, k;\n"
+   "}",
+   Style);
+
+  verifyFormat("struct foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "struct bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("class foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "class bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("namespace foo {\n"
+   "  int i, j;\n"
+   "}\n"
+   "\n"
+   "namespace bar {\n"
+   "  int j, k;\n"
+ 

[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-31 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396781.
ksyx marked 5 inline comments as done.
ksyx added a comment.

Change lambda variable names' leading letter to uppercase.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/lib/Format/WhitespaceManager.h
  clang/unittests/Format/CMakeLists.txt
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- /dev/null
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -0,0 +1,316 @@
+//===- DefinitionBlockSeparatorTest.cpp - Formatting unit tests ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "FormatTestUtils.h"
+#include "clang/Format/Format.h"
+
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "definition-block-separator-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class DefinitionBlockSeparatorTest : public ::testing::Test {
+protected:
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const std::vector ,
+   const FormatStyle  = getLLVMStyle()) {
+LLVM_DEBUG(llvm::errs() << "---\n");
+LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+tooling::Replacements Replaces =
+clang::format::separateDefinitionBlocks(Style, Code, Ranges, "");
+auto Result = applyAllReplacements(Code, Replaces);
+EXPECT_TRUE(static_cast(Result));
+LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+return *Result;
+  }
+
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle()) {
+return separateDefinitionBlocks(
+Code,
+/*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
+  }
+
+  static void verifyFormat(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle(),
+   llvm::StringRef ExpectedCode = "") {
+bool HasOriginalCode = true;
+if (ExpectedCode == "") {
+  ExpectedCode = Code;
+  HasOriginalCode = false;
+}
+
+FormatStyle InverseStyle = Style;
+if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always)
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
+else
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+EXPECT_EQ(ExpectedCode.str(), separateDefinitionBlocks(ExpectedCode, Style))
+<< "Expected code is not stable";
+std::string InverseResult = separateDefinitionBlocks(Code, InverseStyle);
+EXPECT_NE(Code.str(), InverseResult)
+<< "Inverse formatting makes no difference";
+std::string CodeToFormat =
+HasOriginalCode ? Code.str() : removeEmptyLines(Code);
+std::string Result = separateDefinitionBlocks(CodeToFormat, Style);
+EXPECT_EQ(ExpectedCode.str(), Result) << "Test failed. Formatted:\n"
+  << Result;
+  }
+
+  static std::string removeEmptyLines(llvm::StringRef Code) {
+std::string Result = "";
+for (auto Char : Code.str()) {
+  if (Result.size()) {
+auto LastChar = Result.back();
+if ((Char == '\n' && LastChar == '\n') ||
+(Char == '\r' && (LastChar == '\r' || LastChar == '\n')))
+  continue;
+  }
+  Result.push_back(Char);
+}
+return Result;
+  }
+};
+
+TEST_F(DefinitionBlockSeparatorTest, Basic) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  verifyFormat("int foo() {\n"
+   "  int i, j;\n"
+   "}\n"
+   "\n"
+   "int bar() {\n"
+   "  int j, k;\n"
+   "}",
+   Style);
+
+  verifyFormat("struct foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "struct bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("class foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "class bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("namespace foo {\n"
+   "  int i, j;\n"
+   "}\n"
+   "\n"
+   "namespace bar {\n"
+  

[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-30 Thread ksyx via Phabricator via cfe-commits
ksyx added inline comments.



Comment at: clang/include/clang/Format/Format.h:4097
+   ArrayRef Ranges,
+   StringRef FileName = "");
+

HazardyKnusperkeks wrote:
> The only use of this function I found is in the tests and it sets the 
> argument, or am I mistaken?
I suppose this function might also be called from some users of the library 
other than clangfmt itself? Besides, the declarations nearby are setting this 
default value.



Comment at: clang/lib/Format/DefinitionBlockSeparator.cpp:35
+return;
+  auto likelyDefinition = [this](AnnotatedLine *Line) {
+if (Line->MightBeFunctionDecl && Line->mightBeFunctionDefinition())

HazardyKnusperkeks wrote:
> 
[[ 
https://github.com/llvm/llvm-project/blob/298367ee6e36eeb1b193ad9fa92082c2ef2345a3/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp#L2393
 | Some other lambdas ]] I found use lowercased leading letter, as it looks 
like a function (call).


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

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


[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-30 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396727.
ksyx marked 9 inline comments as done.
ksyx added a comment.

- Apply review suggestions.
- Assert the style is not `SDS_Leave` in private method, while return no change 
in public method if so.
- Rename loop variable `Line` to `CurrentLine`


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/lib/Format/WhitespaceManager.h
  clang/unittests/Format/CMakeLists.txt
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- /dev/null
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -0,0 +1,316 @@
+//===- DefinitionBlockSeparatorTest.cpp - Formatting unit tests ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "FormatTestUtils.h"
+#include "clang/Format/Format.h"
+
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "definition-block-separator-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class DefinitionBlockSeparatorTest : public ::testing::Test {
+protected:
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const std::vector ,
+   const FormatStyle  = getLLVMStyle()) {
+LLVM_DEBUG(llvm::errs() << "---\n");
+LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+tooling::Replacements Replaces =
+clang::format::separateDefinitionBlocks(Style, Code, Ranges, "");
+auto Result = applyAllReplacements(Code, Replaces);
+EXPECT_TRUE(static_cast(Result));
+LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+return *Result;
+  }
+
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle()) {
+return separateDefinitionBlocks(
+Code,
+/*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
+  }
+
+  static void verifyFormat(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle(),
+   llvm::StringRef ExpectedCode = "") {
+bool HasOriginalCode = true;
+if (ExpectedCode == "") {
+  ExpectedCode = Code;
+  HasOriginalCode = false;
+}
+
+FormatStyle InverseStyle = Style;
+if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always)
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
+else
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+EXPECT_EQ(ExpectedCode.str(), separateDefinitionBlocks(ExpectedCode, Style))
+<< "Expected code is not stable";
+std::string InverseResult = separateDefinitionBlocks(Code, InverseStyle);
+EXPECT_NE(Code.str(), InverseResult)
+<< "Inverse formatting makes no difference";
+std::string CodeToFormat =
+HasOriginalCode ? Code.str() : removeEmptyLines(Code);
+std::string Result = separateDefinitionBlocks(CodeToFormat, Style);
+EXPECT_EQ(ExpectedCode.str(), Result) << "Test failed. Formatted:\n"
+  << Result;
+  }
+
+  static std::string removeEmptyLines(llvm::StringRef Code) {
+std::string Result = "";
+for (auto Char : Code.str()) {
+  if (Result.size()) {
+auto LastChar = Result.back();
+if ((Char == '\n' && LastChar == '\n') ||
+(Char == '\r' && (LastChar == '\r' || LastChar == '\n')))
+  continue;
+  }
+  Result.push_back(Char);
+}
+return Result;
+  }
+};
+
+TEST_F(DefinitionBlockSeparatorTest, Basic) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  verifyFormat("int foo() {\n"
+   "  int i, j;\n"
+   "}\n"
+   "\n"
+   "int bar() {\n"
+   "  int j, k;\n"
+   "}",
+   Style);
+
+  verifyFormat("struct foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "struct bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("class foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "class bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("namespace foo 

[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-30 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396709.
ksyx marked 2 inline comments as done.
ksyx added a comment.

minor fixes.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/lib/Format/WhitespaceManager.h
  clang/unittests/Format/CMakeLists.txt
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- /dev/null
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -0,0 +1,316 @@
+//===- DefinitionBlockSeparatorTest.cpp - Formatting unit tests ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "FormatTestUtils.h"
+#include "clang/Format/Format.h"
+
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "definition-block-separator-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class DefinitionBlockSeparatorTest : public ::testing::Test {
+protected:
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const std::vector ,
+   const FormatStyle  = getLLVMStyle()) {
+LLVM_DEBUG(llvm::errs() << "---\n");
+LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+tooling::Replacements Replaces =
+clang::format::separateDefinitionBlocks(Style, Code, Ranges, "");
+auto Result = applyAllReplacements(Code, Replaces);
+EXPECT_TRUE(static_cast(Result));
+LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+return *Result;
+  }
+
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle()) {
+return separateDefinitionBlocks(
+Code,
+/*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
+  }
+
+  static void verifyFormat(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle(),
+   llvm::StringRef ExpectedCode = "") {
+bool HasOriginalCode = true;
+if (ExpectedCode == "") {
+  ExpectedCode = Code;
+  HasOriginalCode = false;
+}
+
+FormatStyle InverseStyle = Style;
+if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always)
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
+else
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+EXPECT_EQ(ExpectedCode.str(), separateDefinitionBlocks(ExpectedCode, Style))
+<< "Expected code is not stable";
+std::string InverseResult = separateDefinitionBlocks(Code, InverseStyle);
+EXPECT_NE(Code.str(), InverseResult)
+<< "Inverse formatting makes no difference";
+std::string CodeToFormat =
+HasOriginalCode ? Code.str() : removeEmptyLines(Code);
+std::string Result = separateDefinitionBlocks(CodeToFormat, Style);
+EXPECT_EQ(ExpectedCode.str(), Result) << "Test failed. Formatted:\n"
+  << Result;
+  }
+
+  static std::string removeEmptyLines(llvm::StringRef Code) {
+std::string Result = "";
+for (auto Char : Code.str()) {
+  if (Result.size()) {
+auto LastChar = Result.back();
+if ((Char == '\n' && LastChar == '\n') ||
+(Char == '\r' && (LastChar == '\r' || LastChar == '\n')))
+  continue;
+  }
+  Result.push_back(Char);
+}
+return Result;
+  }
+};
+
+TEST_F(DefinitionBlockSeparatorTest, Basic) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  verifyFormat("int foo() {\n"
+   "  int i, j;\n"
+   "}\n"
+   "\n"
+   "int bar() {\n"
+   "  int j, k;\n"
+   "}",
+   Style);
+
+  verifyFormat("struct foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "struct bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("class foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "class bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("namespace foo {\n"
+   "  int i, j;\n"
+   "}\n"
+   "\n"
+   "namespace bar {\n"
+   "  int j, k;\n"
+   "}",

[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-29 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396577.
ksyx added a comment.

Apply suggestion from clangfmt


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/lib/Format/WhitespaceManager.h
  clang/unittests/Format/CMakeLists.txt
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- /dev/null
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -0,0 +1,316 @@
+//===- DefinitionBlockSeparatorTest.cpp - Formatting unit tests ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "FormatTestUtils.h"
+#include "clang/Format/Format.h"
+
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "definition-block-separator-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class DefinitionBlockSeparatorTest : public ::testing::Test {
+protected:
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const std::vector ,
+   const FormatStyle  = getLLVMStyle()) {
+LLVM_DEBUG(llvm::errs() << "---\n");
+LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+tooling::Replacements Replaces =
+clang::format::separateDefinitionBlocks(Style, Code, Ranges, "");
+auto Result = applyAllReplacements(Code, Replaces);
+EXPECT_TRUE(static_cast(Result));
+LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+return *Result;
+  }
+
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle()) {
+return separateDefinitionBlocks(
+Code,
+/*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
+  }
+
+  static void verifyFormat(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle(),
+   llvm::StringRef ExpectedCode = "") {
+bool HasOriginalCode = true;
+if (ExpectedCode == "") {
+  ExpectedCode = Code;
+  HasOriginalCode = false;
+}
+
+FormatStyle InverseStyle = Style;
+if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always)
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
+else
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+EXPECT_EQ(ExpectedCode.str(), separateDefinitionBlocks(ExpectedCode, Style))
+<< "Expected code is not stable";
+std::string InverseResult = separateDefinitionBlocks(Code, InverseStyle);
+EXPECT_NE(Code.str(), InverseResult)
+<< "Inverse formatting makes no difference";
+std::string CodeToFormat =
+HasOriginalCode ? Code.str() : removeEmptyLines(Code);
+std::string Result = separateDefinitionBlocks(CodeToFormat, Style);
+EXPECT_EQ(ExpectedCode.str(), Result) << "Test failed. Formatted:\n"
+  << Result;
+  }
+
+  static std::string removeEmptyLines(llvm::StringRef Code) {
+std::string Result = "";
+for (auto Char : Code.str()) {
+  if (Result.size()) {
+auto LastChar = Result.back();
+if ((Char == '\n' && LastChar == '\n') ||
+(Char == '\r' && (LastChar == '\r' || LastChar == '\n')))
+  continue;
+  }
+  Result.push_back(Char);
+}
+return Result;
+  }
+};
+
+TEST_F(DefinitionBlockSeparatorTest, Basic) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  verifyFormat("int foo() {\n"
+   "  int i, j;\n"
+   "}\n"
+   "\n"
+   "int bar() {\n"
+   "  int j, k;\n"
+   "}",
+   Style);
+
+  verifyFormat("struct foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "struct bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("class foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "class bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("namespace foo {\n"
+   "  int i, j;\n"
+   "}\n"
+   "\n"
+   "namespace bar {\n"
+   "  int j, k;\n"
+   "}",
+   

[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-29 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396575.
ksyx added a comment.

Apply clangfmt's suggestions


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/lib/Format/WhitespaceManager.h
  clang/unittests/Format/CMakeLists.txt
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- /dev/null
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -0,0 +1,316 @@
+//===- DefinitionBlockSeparatorTest.cpp - Formatting unit tests ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "clang/Format/Format.h"
+#include "FormatTestUtils.h"
+
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "definition-block-separator-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class DefinitionBlockSeparatorTest : public ::testing::Test {
+protected:
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const std::vector ,
+   const FormatStyle  = getLLVMStyle()) {
+LLVM_DEBUG(llvm::errs() << "---\n");
+LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+tooling::Replacements Replaces =
+clang::format::separateDefinitionBlocks(Style, Code, Ranges, "");
+auto Result = applyAllReplacements(Code, Replaces);
+EXPECT_TRUE(static_cast(Result));
+LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+return *Result;
+  }
+
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle()) {
+return separateDefinitionBlocks(
+Code,
+/*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
+  }
+
+  static void verifyFormat(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle(),
+   llvm::StringRef ExpectedCode = "") {
+bool HasOriginalCode = true;
+if (ExpectedCode == "") {
+  ExpectedCode = Code;
+  HasOriginalCode = false;
+}
+
+FormatStyle InverseStyle = Style;
+if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always)
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
+else
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+EXPECT_EQ(ExpectedCode.str(), separateDefinitionBlocks(ExpectedCode, Style))
+<< "Expected code is not stable";
+std::string InverseResult = separateDefinitionBlocks(Code, InverseStyle);
+EXPECT_NE(Code.str(), InverseResult)
+<< "Inverse formatting makes no difference";
+std::string CodeToFormat =
+HasOriginalCode ? Code.str() : removeEmptyLines(Code);
+std::string Result = separateDefinitionBlocks(CodeToFormat, Style);
+EXPECT_EQ(ExpectedCode.str(), Result) << "Test failed. Formatted:\n"
+  << Result;
+  }
+
+  static std::string removeEmptyLines(llvm::StringRef Code) {
+std::string Result = "";
+  for (auto Char : Code.str()) {
+if (Result.size()) {
+  auto LastChar = Result.back();
+  if ((Char == '\n' && LastChar == '\n') ||
+  (Char == '\r' && (LastChar == '\r' || LastChar == '\n')))
+continue;
+}
+Result.push_back(Char);
+  }
+return Result;
+  }
+};
+
+TEST_F(DefinitionBlockSeparatorTest, Basic) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  verifyFormat("int foo() {\n"
+   "  int i, j;\n"
+   "}\n"
+   "\n"
+   "int bar() {\n"
+   "  int j, k;\n"
+   "}",
+   Style);
+
+  verifyFormat("struct foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "struct bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("class foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "class bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("namespace foo {\n"
+   "  int i, j;\n"
+   "}\n"
+   "\n"
+   "namespace bar {\n"
+   "  int j, k;\n"
+   "}",
+

[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-29 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396574.
ksyx marked 9 inline comments as done.
ksyx edited the summary of this revision.
ksyx added a comment.

- Improve compatibility to other languages
- Improve unit test by test format stability, inverse result, and generate some 
input from expected output
- Reformat sample code
- Use more concise word for SDS_Leave


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/lib/Format/WhitespaceManager.h
  clang/unittests/Format/CMakeLists.txt
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- /dev/null
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -0,0 +1,316 @@
+//===- DefinitionBlockSeparatorTest.cpp - Formatting unit tests ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "clang/Format/Format.h"
+#include "FormatTestUtils.h"
+
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "definition-block-separator-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class DefinitionBlockSeparatorTest : public ::testing::Test {
+protected:
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const std::vector ,
+   const FormatStyle  = getLLVMStyle()) {
+LLVM_DEBUG(llvm::errs() << "---\n");
+LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+tooling::Replacements Replaces =
+clang::format::separateDefinitionBlocks(Style, Code, Ranges, "");
+auto Result = applyAllReplacements(Code, Replaces);
+EXPECT_TRUE(static_cast(Result));
+LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+return *Result;
+  }
+
+  static std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle()) {
+return separateDefinitionBlocks(
+Code,
+/*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
+  }
+
+  static void verifyFormat(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle(),
+   llvm::StringRef ExpectedCode = "") {
+bool HasOriginalCode = true;
+if (ExpectedCode == "") {
+  ExpectedCode = Code;
+  HasOriginalCode = false;
+}
+
+FormatStyle InverseStyle = Style;
+if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always)
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
+else
+  InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+EXPECT_EQ(ExpectedCode.str(), separateDefinitionBlocks(ExpectedCode, Style))
+<< "Expected code is not stable";
+std::string InverseResult = separateDefinitionBlocks(Code, InverseStyle);
+EXPECT_NE(Code.str(), InverseResult)
+<< "Inverse formatting makes no difference";
+std::string CodeToFormat =
+HasOriginalCode ? Code.str() : removeEmptyLines(Code);
+std::string Result = separateDefinitionBlocks(CodeToFormat, Style);
+EXPECT_EQ(ExpectedCode.str(), Result) << "Test failed. Formatted:\n"
+  << Result;
+  }
+
+  static std::string removeEmptyLines(llvm::StringRef Code) {
+std::string Result = "";
+  for (auto Char : Code.str()) {
+if (Result.size()) {
+  auto LastChar = Result.back();
+  if ((Char == '\n' && LastChar == '\n') ||
+  (Char == '\r' && (LastChar == '\r' || LastChar == '\n')))
+continue;
+}
+Result.push_back(Char);
+  }
+return Result;
+  }
+};
+
+TEST_F(DefinitionBlockSeparatorTest, Basic) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  verifyFormat("int foo() {\n"
+   "  int i, j;\n"
+   "}\n"
+   "\n"
+   "int bar() {\n"
+   "  int j, k;\n"
+   "}",
+   Style);
+
+  verifyFormat("struct foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "struct bar {\n"
+   "  int j, k;\n"
+   "};",
+   Style);
+
+  verifyFormat("class foo {\n"
+   "  int i, j;\n"
+   "};\n"
+   "\n"
+   "class bar {\n"
+   "  

[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-28 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396430.
ksyx added a comment.

Fix typo.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/lib/Format/WhitespaceManager.h
  clang/unittests/Format/CMakeLists.txt
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- /dev/null
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -0,0 +1,281 @@
+//===- DefinitionBlockSeparatorTest.cpp - Formatting unit tests ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "clang/Format/Format.h"
+
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "definition-block-separator-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class DefinitionBlockSeparatorTest : public ::testing::Test {
+protected:
+  std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const std::vector ,
+   const FormatStyle  = getLLVMStyle()) {
+LLVM_DEBUG(llvm::errs() << "---\n");
+LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+tooling::Replacements Replaces =
+clang::format::separateDefinitionBlocks(Style, Code, Ranges, "");
+auto Result = applyAllReplacements(Code, Replaces);
+EXPECT_TRUE(static_cast(Result));
+LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+return *Result;
+  }
+
+  std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle()) {
+return separateDefinitionBlocks(
+Code,
+/*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
+  }
+};
+
+TEST_F(DefinitionBlockSeparatorTest, Basic) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  EXPECT_EQ("int foo() {\n"
+"  int i, j;\n"
+"}\n"
+"\n"
+"int bar() {\n"
+"  int j, k;\n"
+"}",
+separateDefinitionBlocks("int foo() {\n"
+ "  int i, j;\n"
+ "}\n"
+ "int bar() {\n"
+ "  int j, k;\n"
+ "}",
+ Style));
+
+  EXPECT_EQ("struct foo {\n"
+"  int i, j;\n"
+"};\n"
+"\n"
+"struct bar {\n"
+"  int j, k;\n"
+"};",
+separateDefinitionBlocks("struct foo {\n"
+ "  int i, j;\n"
+ "};\n"
+ "struct bar {\n"
+ "  int j, k;\n"
+ "};",
+ Style));
+
+  EXPECT_EQ("class foo {\n"
+"  int i, j;\n"
+"};\n"
+"\n"
+"class bar {\n"
+"  int j, k;\n"
+"};",
+separateDefinitionBlocks("class foo {\n"
+ "  int i, j;\n"
+ "};\n"
+ "class bar {\n"
+ "  int j, k;\n"
+ "};",
+ Style));
+
+  EXPECT_EQ("namespace foo {\n"
+"  int i, j;\n"
+"}\n"
+"\n"
+"namespace bar {\n"
+"  int j, k;\n"
+"}",
+separateDefinitionBlocks("namespace foo {\n"
+ "  int i, j;\n"
+ "}\n"
+ "namespace bar {\n"
+ "  int j, k;\n"
+ "}",
+ Style));
+
+  EXPECT_EQ("enum Foo {\n"
+"  FOO, BAR\n"
+"};\n"
+"\n"
+"enum Bar {\n"
+"  FOOBAR, BARFOO\n"
+"};",
+separateDefinitionBlocks("enum Foo {\n"
+ "  FOO, BAR\n"
+ "};\n"
+ "enum Bar {\n"
+ 

[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-28 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396428.
ksyx marked 5 inline comments as done.
ksyx added a comment.

- Apply suggestions from clangfmt
- Add missing period of comments
- Fix namespace ending comment
- Add comment for notes on return value of WhitespaceManager's method


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/lib/Format/WhitespaceManager.h
  clang/unittests/Format/CMakeLists.txt
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- /dev/null
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -0,0 +1,281 @@
+//===- DefinitionBlockSeparatorTest.cpp - Formatting unit tests ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "clang/Format/Format.h"
+
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "definition-block-separator-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class DefinitionBlockSeparatorTest : public ::testing::Test {
+protected:
+  std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const std::vector ,
+   const FormatStyle  = getLLVMStyle()) {
+LLVM_DEBUG(llvm::errs() << "---\n");
+LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+tooling::Replacements Replaces =
+clang::format::separateDefinitionBlocks(Style, Code, Ranges, "");
+auto Result = applyAllReplacements(Code, Replaces);
+EXPECT_TRUE(static_cast(Result));
+LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+return *Result;
+  }
+
+  std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle()) {
+return separateDefinitionBlocks(
+Code,
+/*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
+  }
+};
+
+TEST_F(DefinitionBlockSeparatorTest, Basic) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  EXPECT_EQ("int foo() {\n"
+"  int i, j;\n"
+"}\n"
+"\n"
+"int bar() {\n"
+"  int j, k;\n"
+"}",
+separateDefinitionBlocks("int foo() {\n"
+ "  int i, j;\n"
+ "}\n"
+ "int bar() {\n"
+ "  int j, k;\n"
+ "}",
+ Style));
+
+  EXPECT_EQ("struct foo {\n"
+"  int i, j;\n"
+"};\n"
+"\n"
+"struct bar {\n"
+"  int j, k;\n"
+"};",
+separateDefinitionBlocks("struct foo {\n"
+ "  int i, j;\n"
+ "};\n"
+ "struct bar {\n"
+ "  int j, k;\n"
+ "};",
+ Style));
+
+  EXPECT_EQ("class foo {\n"
+"  int i, j;\n"
+"};\n"
+"\n"
+"class bar {\n"
+"  int j, k;\n"
+"};",
+separateDefinitionBlocks("class foo {\n"
+ "  int i, j;\n"
+ "};\n"
+ "class bar {\n"
+ "  int j, k;\n"
+ "};",
+ Style));
+
+  EXPECT_EQ("namespace foo {\n"
+"  int i, j;\n"
+"}\n"
+"\n"
+"namespace bar {\n"
+"  int j, k;\n"
+"}",
+separateDefinitionBlocks("namespace foo {\n"
+ "  int i, j;\n"
+ "}\n"
+ "namespace bar {\n"
+ "  int j, k;\n"
+ "}",
+ Style));
+
+  EXPECT_EQ("enum Foo {\n"
+"  FOO, BAR\n"
+"};\n"
+"\n"
+"enum Bar {\n"
+"  FOOBAR, BARFOO\n"
+"};",
+separateDefinitionBlocks("enum 

[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-28 Thread ksyx via Phabricator via cfe-commits
ksyx marked an inline comment as done.
ksyx added inline comments.



Comment at: clang/lib/Format/Format.cpp:2114
+  if (Result.add(R))
+return;
+  }

HazardyKnusperkeks wrote:
> Why only add the first replacement?
This worked well as the `Error` class `add` method returned has overloaded 
boolean operator, returning true (nonzero) when failing, which simulates 
program exit code scheme? (ref: [[ 
https://llvm.org/doxygen/classllvm_1_1Error.html#a981b4992a3b7cce718c7995a7d6193a0
 | Doxygen/Error/operator bool ]])


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

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


[PATCH] D116314: [clang-format] Add style to separate definition blocks

2021-12-28 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396421.
ksyx marked 4 inline comments as done.
ksyx added a comment.
Herald added a subscriber: mgorny.

- Change version from 15 to 14
- Add option `SDS_Never`, rename `SDS_Between` => `SDS_Always`
- Add unit tests
- Separate class into new file


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/DefinitionBlockSeparator.cpp
  clang/lib/Format/DefinitionBlockSeparator.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/lib/Format/WhitespaceManager.h
  clang/unittests/Format/CMakeLists.txt
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===
--- /dev/null
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -0,0 +1,273 @@
+//===- DefinitionBlockSeparatorTest.cpp - Formatting unit tests ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "clang/Format/Format.h"
+
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "definition-block-separator-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class DefinitionBlockSeparatorTest : public ::testing::Test {
+protected:
+  std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const std::vector ,
+   const FormatStyle  = getLLVMStyle()) {
+LLVM_DEBUG(llvm::errs() << "---\n");
+LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+tooling::Replacements Replaces =
+clang::format::separateDefinitionBlocks(Style, Code, Ranges, "");
+auto Result = applyAllReplacements(Code, Replaces);
+EXPECT_TRUE(static_cast(Result));
+LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+return *Result;
+  }
+
+  std::string
+  separateDefinitionBlocks(llvm::StringRef Code,
+   const FormatStyle  = getLLVMStyle()) {
+return separateDefinitionBlocks(
+Code,
+/*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
+  }
+};
+
+TEST_F(DefinitionBlockSeparatorTest, Basic) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  EXPECT_EQ("int foo() {\n"
+"  int i, j;\n"
+"}\n"
+"\n"
+"int bar() {\n"
+"  int j, k;\n"
+"}",
+separateDefinitionBlocks("int foo() {\n"
+ "  int i, j;\n"
+ "}\n"
+ "int bar() {\n"
+ "  int j, k;\n"
+ "}", Style));
+
+  EXPECT_EQ("struct foo {\n"
+"  int i, j;\n"
+"};\n"
+"\n"
+"struct bar {\n"
+"  int j, k;\n"
+"};",
+separateDefinitionBlocks("struct foo {\n"
+ "  int i, j;\n"
+ "};\n"
+ "struct bar {\n"
+ "  int j, k;\n"
+ "};", Style));
+
+  EXPECT_EQ("class foo {\n"
+"  int i, j;\n"
+"};\n"
+"\n"
+"class bar {\n"
+"  int j, k;\n"
+"};",
+separateDefinitionBlocks("class foo {\n"
+ "  int i, j;\n"
+ "};\n"
+ "class bar {\n"
+ "  int j, k;\n"
+ "};", Style));
+
+  EXPECT_EQ("namespace foo {\n"
+"  int i, j;\n"
+"}\n"
+"\n"
+"namespace bar {\n"
+"  int j, k;\n"
+"}",
+separateDefinitionBlocks("namespace foo {\n"
+ "  int i, j;\n"
+ "}\n"
+ "namespace bar {\n"
+ "  int j, k;\n"
+ "}", Style));
+
+  EXPECT_EQ("enum Foo {\n"
+"  FOO, BAR\n"
+"};\n"
+"\n"
+"enum Bar {\n"
+"  FOOBAR, BARFOO\n"
+"};",
+separateDefinitionBlocks("enum Foo {\n"
+ "  FOO, BAR\n"
+ "};\n"
+ 

[PATCH] D116314: [Clangfmt] Add style to separate definition blocks

2021-12-27 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396358.
ksyx marked an inline comment as done.
ksyx added a comment.

- Change bool type of the option to enum type
- Generate empty line for enum block

Testfile:

  // test.cpp
  #include 
  #include 
  #include 
  struct Foo {
int a,b,c;
  };
  namespace Ns {
  class Bar {
  public:
struct Foobar {
  int a;
  int b;
};
  private:
int t;
int method1()
{
  // Intentional test for different
  // line breaking mode of brackets
}
template
int method2(T x) {
  // ...
}
enum Foo {
  FOO,
  BAR
};
int method3(int par) {}
  };
  class C {
  };
  }
  namespace Ns2 {
  
  }

Tests:

  bin/clang-format test2.cpp # Test default (leave as it is)
  bin/clang-format -style='{SeparateDefinitionBlocks: Between}' test2.cpp # 
Test normal functionality
  bin/clang-format -style='{SeparateDefinitionBlocks: Leave}' test2.cpp # Test 
turning off
  bin/clang-format -style='{SeparateDefinitionBlocks: Between, BasedOnStyle: 
WebKit}' test2.cpp # Case: opening bracket { has   its own line


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp

Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -450,6 +450,14 @@
   }
 };
 
+template <>
+struct ScalarEnumerationTraits {
+  static void enumeration(IO , FormatStyle::SeparateDefinitionStyle ) {
+IO.enumCase(Value, "Leave", FormatStyle::SDS_Leave);
+IO.enumCase(Value, "Between", FormatStyle::SDS_Between);
+  }
+};
+
 template <>
 struct ScalarEnumerationTraits {
   static void enumeration(IO , FormatStyle::SpaceBeforeParensStyle ) {
@@ -770,6 +778,7 @@
 IO.mapOptional("ReferenceAlignment", Style.ReferenceAlignment);
 IO.mapOptional("ReflowComments", Style.ReflowComments);
 IO.mapOptional("ShortNamespaceLines", Style.ShortNamespaceLines);
+IO.mapOptional("SeparateDefinitionBlocks", Style.SeparateDefinitionBlocks);
 IO.mapOptional("SortIncludes", Style.SortIncludes);
 IO.mapOptional("SortJavaStaticImport", Style.SortJavaStaticImport);
 IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
@@ -1193,6 +1202,7 @@
   LLVMStyle.ObjCSpaceBeforeProtocolList = true;
   LLVMStyle.PointerAlignment = FormatStyle::PAS_Right;
   LLVMStyle.ReferenceAlignment = FormatStyle::RAS_Pointer;
+  LLVMStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Leave;
   LLVMStyle.ShortNamespaceLines = 1;
   LLVMStyle.SpacesBeforeTrailingComments = 1;
   LLVMStyle.Standard = FormatStyle::LS_Latest;
@@ -1863,13 +1873,13 @@
 return std::make_pair(Result, Penalty);
   }
 
-private:
   static bool inputUsesCRLF(StringRef Text, bool DefaultToCRLF) {
 size_t LF = Text.count('\n');
 size_t CR = Text.count('\r') * 2;
 return LF == CR ? DefaultToCRLF : CR > LF;
   }
 
+private:
   bool
   hasCpp03IncompatibleFormat(const SmallVectorImpl ) {
 for (const AnnotatedLine *Line : Lines) {
@@ -2012,6 +2022,99 @@
   }
 };
 
+// This class separates definition blocks like classes, functions, and
+// namespaces by inserting a line break between them.
+class DefinitionBlockSeparator : public TokenAnalyzer {
+public:
+  DefinitionBlockSeparator(const Environment , const FormatStyle )
+  : TokenAnalyzer(Env, Style) {}
+
+  std::pair
+  analyze(TokenAnnotator ,
+  SmallVectorImpl ,
+  FormatTokenLexer ) override {
+AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
+tooling::Replacements Result;
+separateBlocks(AnnotatedLines, Result);
+return {Result, 0};
+  }
+
+private:
+  void separateBlocks(SmallVectorImpl ,
+  tooling::Replacements ) {
+auto likelyDefinition = [](AnnotatedLine *Line) {
+  return (Line->MightBeFunctionDecl && Line->mightBeFunctionDefinition()) ||
+ Line->First->isOneOf(tok::kw_class, tok::kw_struct,
+  tok::kw_namespace, tok::kw_enum);
+};
+WhitespaceManager Whitespaces(
+Env.getSourceManager(), Style,
+Style.DeriveLineEnding
+? Formatter::inputUsesCRLF(
+  Env.getSourceManager().getBufferData(Env.getFileID()),
+  Style.UseCRLF)
+: Style.UseCRLF);
+for (unsigned I = 0; I < Lines.size(); I++) {
+  auto Line = Lines[I];
+  if (Line->First->closesScope()) {
+auto OpeningLineIndex = Line->MatchingOpeningBlockLineIndex;
+// Case: Opening bracket has its own line
+if (OpeningLineIndex > 0 &&
+Lines[OpeningLineIndex]->First->TokenText == "{") {
+  OpeningLineIndex--;
+}
+AnnotatedLine *OpeningLine = Lines[OpeningLineIndex];
+// Closing a function definition
+if (likelyDefinition(OpeningLine)) 

[PATCH] D116314: [Clangfmt] Add style to separate definition blocks

2021-12-27 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396344.
ksyx added a comment.

Fix missing colon in sample/test code


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp

Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -770,6 +770,7 @@
 IO.mapOptional("ReferenceAlignment", Style.ReferenceAlignment);
 IO.mapOptional("ReflowComments", Style.ReflowComments);
 IO.mapOptional("ShortNamespaceLines", Style.ShortNamespaceLines);
+IO.mapOptional("SeparateDefinitionBlocks", Style.SeparateDefinitionBlocks);
 IO.mapOptional("SortIncludes", Style.SortIncludes);
 IO.mapOptional("SortJavaStaticImport", Style.SortJavaStaticImport);
 IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
@@ -1193,6 +1194,7 @@
   LLVMStyle.ObjCSpaceBeforeProtocolList = true;
   LLVMStyle.PointerAlignment = FormatStyle::PAS_Right;
   LLVMStyle.ReferenceAlignment = FormatStyle::RAS_Pointer;
+  LLVMStyle.SeparateDefinitionBlocks = true;
   LLVMStyle.ShortNamespaceLines = 1;
   LLVMStyle.SpacesBeforeTrailingComments = 1;
   LLVMStyle.Standard = FormatStyle::LS_Latest;
@@ -1333,6 +1335,7 @@
   /*BasedOnStyle=*/"google",
   },
   };
+  GoogleStyle.SeparateDefinitionBlocks = false;
   GoogleStyle.SpacesBeforeTrailingComments = 2;
   GoogleStyle.Standard = FormatStyle::LS_Auto;
 
@@ -1863,13 +1866,13 @@
 return std::make_pair(Result, Penalty);
   }
 
-private:
   static bool inputUsesCRLF(StringRef Text, bool DefaultToCRLF) {
 size_t LF = Text.count('\n');
 size_t CR = Text.count('\r') * 2;
 return LF == CR ? DefaultToCRLF : CR > LF;
   }
 
+private:
   bool
   hasCpp03IncompatibleFormat(const SmallVectorImpl ) {
 for (const AnnotatedLine *Line : Lines) {
@@ -2012,6 +2015,98 @@
   }
 };
 
+// This class separates definition blocks like classes, functions, and
+// namespaces by inserting a line break between them.
+class DefinitionBlockSeparator : public TokenAnalyzer {
+public:
+  DefinitionBlockSeparator(const Environment , const FormatStyle )
+  : TokenAnalyzer(Env, Style) {}
+
+  std::pair
+  analyze(TokenAnnotator ,
+  SmallVectorImpl ,
+  FormatTokenLexer ) override {
+AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
+tooling::Replacements Result;
+separateBlocks(AnnotatedLines, Result);
+return {Result, 0};
+  }
+
+private:
+  void separateBlocks(SmallVectorImpl ,
+  tooling::Replacements ) {
+auto likelyDefinition = [](AnnotatedLine *Line) {
+  return (Line->MightBeFunctionDecl && Line->mightBeFunctionDefinition()) ||
+ Line->First->isOneOf(tok::kw_class, tok::kw_struct,
+  tok::kw_namespace);
+};
+WhitespaceManager Whitespaces(
+Env.getSourceManager(), Style,
+Style.DeriveLineEnding
+? Formatter::inputUsesCRLF(
+  Env.getSourceManager().getBufferData(Env.getFileID()),
+  Style.UseCRLF)
+: Style.UseCRLF);
+for (unsigned I = 0; I < Lines.size(); I++) {
+  auto Line = Lines[I];
+  if (Line->First->closesScope()) {
+auto OpeningLineIndex = Line->MatchingOpeningBlockLineIndex;
+// Case: Opening bracket has its own line
+if (OpeningLineIndex > 0 &&
+Lines[OpeningLineIndex]->First->TokenText == "{") {
+  OpeningLineIndex--;
+}
+AnnotatedLine *OpeningLine = Lines[OpeningLineIndex];
+// Closing a function definition
+if (likelyDefinition(OpeningLine)) {
+  FormatToken *TargetToken = nullptr;
+  AnnotatedLine *TargetLine;
+  auto insertReplacement = [&]() {
+assert(TargetToken);
+Whitespaces.replaceWhitespace(*TargetToken, 2,
+  TargetToken->SpacesRequiredBefore,
+  TargetToken->StartsColumn);
+  };
+
+  // Not the first token
+  if (OpeningLineIndex > 0) {
+TargetLine = Lines[OpeningLineIndex - 1];
+// Not immediately following other scopes' opening
+if (TargetLine->Affected && !TargetLine->Last->opensScope()) {
+  // Change target, since we need to put line break *before*
+  // a token
+  TargetLine = OpeningLine;
+  TargetToken = TargetLine->First;
+
+  // Avoid duplicated replacement
+  if (TargetToken && !TargetToken->opensScope()) {
+insertReplacement();
+  }
+}
+  }
+
+  // Not the last token
+  if (I + 1 < Lines.size()) {
+TargetLine = Lines[I + 1];
+TargetToken = 

[PATCH] D116314: [Clangfmt] Add style to separate definition blocks

2021-12-27 Thread ksyx via Phabricator via cfe-commits
ksyx added inline comments.



Comment at: clang/lib/Format/Format.cpp:1197
   LLVMStyle.ReferenceAlignment = FormatStyle::RAS_Pointer;
+  LLVMStyle.SeparateDefinitionBlocks = true;
   LLVMStyle.ShortNamespaceLines = 1;

ksyx wrote:
> Not so sure about this as it would break some tests.
As CI shows some original tests does not expect this empty line, but it looks 
like LLVM is generally adding empty lines between blocks.
```cpp
 template  class B;
+
 class A {
 public:
   int f();
-};
+};\n
```


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

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


[PATCH] D116314: [Clangfmt] Add style to separate definition blocks

2021-12-27 Thread ksyx via Phabricator via cfe-commits
ksyx added inline comments.



Comment at: clang/lib/Format/Format.cpp:1197
   LLVMStyle.ReferenceAlignment = FormatStyle::RAS_Pointer;
+  LLVMStyle.SeparateDefinitionBlocks = true;
   LLVMStyle.ShortNamespaceLines = 1;

Not so sure about this as it would break some tests.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

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


[PATCH] D116314: [Clangfmt] Add style to separate definition blocks

2021-12-27 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 396321.
ksyx added a comment.

Apply clang-format suggestions


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp

Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -770,6 +770,7 @@
 IO.mapOptional("ReferenceAlignment", Style.ReferenceAlignment);
 IO.mapOptional("ReflowComments", Style.ReflowComments);
 IO.mapOptional("ShortNamespaceLines", Style.ShortNamespaceLines);
+IO.mapOptional("SeparateDefinitionBlocks", Style.SeparateDefinitionBlocks);
 IO.mapOptional("SortIncludes", Style.SortIncludes);
 IO.mapOptional("SortJavaStaticImport", Style.SortJavaStaticImport);
 IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
@@ -1193,6 +1194,7 @@
   LLVMStyle.ObjCSpaceBeforeProtocolList = true;
   LLVMStyle.PointerAlignment = FormatStyle::PAS_Right;
   LLVMStyle.ReferenceAlignment = FormatStyle::RAS_Pointer;
+  LLVMStyle.SeparateDefinitionBlocks = true;
   LLVMStyle.ShortNamespaceLines = 1;
   LLVMStyle.SpacesBeforeTrailingComments = 1;
   LLVMStyle.Standard = FormatStyle::LS_Latest;
@@ -1333,6 +1335,7 @@
   /*BasedOnStyle=*/"google",
   },
   };
+  GoogleStyle.SeparateDefinitionBlocks = false;
   GoogleStyle.SpacesBeforeTrailingComments = 2;
   GoogleStyle.Standard = FormatStyle::LS_Auto;
 
@@ -1863,13 +1866,13 @@
 return std::make_pair(Result, Penalty);
   }
 
-private:
   static bool inputUsesCRLF(StringRef Text, bool DefaultToCRLF) {
 size_t LF = Text.count('\n');
 size_t CR = Text.count('\r') * 2;
 return LF == CR ? DefaultToCRLF : CR > LF;
   }
 
+private:
   bool
   hasCpp03IncompatibleFormat(const SmallVectorImpl ) {
 for (const AnnotatedLine *Line : Lines) {
@@ -2012,6 +2015,98 @@
   }
 };
 
+// This class separates definition blocks like classes, functions, and
+// namespaces by inserting a line break between them.
+class DefinitionBlockSeparator : public TokenAnalyzer {
+public:
+  DefinitionBlockSeparator(const Environment , const FormatStyle )
+  : TokenAnalyzer(Env, Style) {}
+
+  std::pair
+  analyze(TokenAnnotator ,
+  SmallVectorImpl ,
+  FormatTokenLexer ) override {
+AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
+tooling::Replacements Result;
+separateBlocks(AnnotatedLines, Result);
+return {Result, 0};
+  }
+
+private:
+  void separateBlocks(SmallVectorImpl ,
+  tooling::Replacements ) {
+auto likelyDefinition = [](AnnotatedLine *Line) {
+  return (Line->MightBeFunctionDecl && Line->mightBeFunctionDefinition()) ||
+ Line->First->isOneOf(tok::kw_class, tok::kw_struct,
+  tok::kw_namespace);
+};
+WhitespaceManager Whitespaces(
+Env.getSourceManager(), Style,
+Style.DeriveLineEnding
+? Formatter::inputUsesCRLF(
+  Env.getSourceManager().getBufferData(Env.getFileID()),
+  Style.UseCRLF)
+: Style.UseCRLF);
+for (unsigned I = 0; I < Lines.size(); I++) {
+  auto Line = Lines[I];
+  if (Line->First->closesScope()) {
+auto OpeningLineIndex = Line->MatchingOpeningBlockLineIndex;
+// Case: Opening bracket has its own line
+if (OpeningLineIndex > 0 &&
+Lines[OpeningLineIndex]->First->TokenText == "{") {
+  OpeningLineIndex--;
+}
+AnnotatedLine *OpeningLine = Lines[OpeningLineIndex];
+// Closing a function definition
+if (likelyDefinition(OpeningLine)) {
+  FormatToken *TargetToken = nullptr;
+  AnnotatedLine *TargetLine;
+  auto insertReplacement = [&]() {
+assert(TargetToken);
+Whitespaces.replaceWhitespace(*TargetToken, 2,
+  TargetToken->SpacesRequiredBefore,
+  TargetToken->StartsColumn);
+  };
+
+  // Not the first token
+  if (OpeningLineIndex > 0) {
+TargetLine = Lines[OpeningLineIndex - 1];
+// Not immediately following other scopes' opening
+if (TargetLine->Affected && !TargetLine->Last->opensScope()) {
+  // Change target, since we need to put line break *before*
+  // a token
+  TargetLine = OpeningLine;
+  TargetToken = TargetLine->First;
+
+  // Avoid duplicated replacement
+  if (TargetToken && !TargetToken->opensScope()) {
+insertReplacement();
+  }
+}
+  }
+
+  // Not the last token
+  if (I + 1 < Lines.size()) {
+TargetLine = Lines[I + 1];
+TargetToken = TargetLine->First;
+
+  

[PATCH] D116314: [Clangfmt] Add style to separate definition blocks

2021-12-27 Thread ksyx via Phabricator via cfe-commits
ksyx added a comment.

Test code:

  // test.cpp
  #include 
  #include 
  #include 
  struct Foo {
int a,b,c;
  };
  namespace Ns {
  class Bar {
  public:
struct Foobar {
  int a;
  int b;
};
  private:
int t;
int method1()
{
  // Intentional test for different
  // line breaking mode of brackets
}
template
int method2(T x) {
  // ...
}
int method3(int par) {}
  };
  class C {
  }
  }
  namespace Ns2 {
  
  }

Tests ran:

  # pwd = build directory
  bin/clang-format test.cpp # LLVM Style
  bin/clang-format -style='{SeparateDefinitionBlocks: false}' test.cpp # 
Disable functionality
  bin/clang-format -style='{BasedOnStyle: Google}' test.cpp # Disable by default
  bin/clang-format -style='{BasedOnStyle: Google, SeparateDefinitionBlocks: 
true}' test.cpp # Override default
  bin/clang-format -style='{BasedOnStyle: WebKit}' test.cpp # Opening bracket 
in new line (WebKit style has this functionality enabled)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116314/new/

https://reviews.llvm.org/D116314

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


[PATCH] D116314: [Clangfmt] Add style to separate definition blocks

2021-12-27 Thread ksyx via Phabricator via cfe-commits
ksyx created this revision.
ksyx added a reviewer: MyDeveloperDay.
ksyx added projects: clang-format, clang.
ksyx requested review of this revision.
Herald added a subscriber: cfe-commits.

This commit resolves GitHub issue #45895 
 (Bugzilla #46550 
), to add empty line between definition blocks 
including namespace, class, and struct.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116314

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp

Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -770,6 +770,7 @@
 IO.mapOptional("ReferenceAlignment", Style.ReferenceAlignment);
 IO.mapOptional("ReflowComments", Style.ReflowComments);
 IO.mapOptional("ShortNamespaceLines", Style.ShortNamespaceLines);
+IO.mapOptional("SeparateDefinitionBlocks", Style.SeparateDefinitionBlocks);
 IO.mapOptional("SortIncludes", Style.SortIncludes);
 IO.mapOptional("SortJavaStaticImport", Style.SortJavaStaticImport);
 IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
@@ -1193,6 +1194,7 @@
   LLVMStyle.ObjCSpaceBeforeProtocolList = true;
   LLVMStyle.PointerAlignment = FormatStyle::PAS_Right;
   LLVMStyle.ReferenceAlignment = FormatStyle::RAS_Pointer;
+  LLVMStyle.SeparateDefinitionBlocks = true;
   LLVMStyle.ShortNamespaceLines = 1;
   LLVMStyle.SpacesBeforeTrailingComments = 1;
   LLVMStyle.Standard = FormatStyle::LS_Latest;
@@ -1333,6 +1335,7 @@
   /*BasedOnStyle=*/"google",
   },
   };
+  GoogleStyle.SeparateDefinitionBlocks = false;
   GoogleStyle.SpacesBeforeTrailingComments = 2;
   GoogleStyle.Standard = FormatStyle::LS_Auto;
 
@@ -1863,13 +1866,13 @@
 return std::make_pair(Result, Penalty);
   }
 
-private:
   static bool inputUsesCRLF(StringRef Text, bool DefaultToCRLF) {
 size_t LF = Text.count('\n');
 size_t CR = Text.count('\r') * 2;
 return LF == CR ? DefaultToCRLF : CR > LF;
   }
 
+private:
   bool
   hasCpp03IncompatibleFormat(const SmallVectorImpl ) {
 for (const AnnotatedLine *Line : Lines) {
@@ -2012,6 +2015,98 @@
   }
 };
 
+// This class separates definition blocks like classes, functions, and
+// namespaces by inserting a line break between them.
+class DefinitionBlockSeparator : public TokenAnalyzer {
+public:
+  DefinitionBlockSeparator(const Environment , const FormatStyle )
+  : TokenAnalyzer(Env, Style) {}
+
+  std::pair
+  analyze(TokenAnnotator ,
+  SmallVectorImpl ,
+  FormatTokenLexer ) override {
+AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
+tooling::Replacements Result;
+separateBlocks(AnnotatedLines, Result);
+return {Result, 0};
+  }
+
+private:
+  void separateBlocks(SmallVectorImpl ,
+  tooling::Replacements ) {
+auto likelyDefinition = [](AnnotatedLine *Line) {
+  return (Line->MightBeFunctionDecl && Line->mightBeFunctionDefinition()) ||
+ Line->First->isOneOf(tok::kw_class, tok::kw_struct,
+  tok::kw_namespace);
+};
+WhitespaceManager Whitespaces(
+Env.getSourceManager(), Style,
+Style.DeriveLineEnding
+? Formatter::inputUsesCRLF(
+  Env.getSourceManager().getBufferData(Env.getFileID()),
+  Style.UseCRLF)
+: Style.UseCRLF);
+for (unsigned I = 0; I < Lines.size(); I++) {
+  auto Line = Lines[I];
+  if (Line->First->closesScope()) {
+auto OpeningLineIndex = Line->MatchingOpeningBlockLineIndex;
+// Case: Opening bracket has its own line
+if (OpeningLineIndex > 0 &&
+Lines[OpeningLineIndex]->First->TokenText == "{") {
+  OpeningLineIndex--;
+}
+AnnotatedLine *OpeningLine = Lines[OpeningLineIndex];
+// Closing a function definition
+if (likelyDefinition(OpeningLine)) {
+  FormatToken *TargetToken = nullptr;
+  AnnotatedLine *TargetLine;
+  auto insertReplacement = [&]() {
+assert(TargetToken);
+Whitespaces.replaceWhitespace(*TargetToken, 2,
+  TargetToken->SpacesRequiredBefore,
+  TargetToken->StartsColumn);
+  };
+
+  // Not the first token
+  if (OpeningLineIndex > 0) {
+TargetLine = Lines[OpeningLineIndex - 1];
+// Not immediately following other scopes' opening
+if (TargetLine->Affected && !TargetLine->Last->opensScope()) {
+  // Change target, since we need to put line break *before*
+  // a token
+  TargetLine = OpeningLine;
+  TargetToken = TargetLine->First;
+
+  // Avoid duplicated replacement
+  if 

[PATCH] D103313: [RISCV][Clang] Implement support for zmmul-experimental

2021-06-10 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 351083.
ksyx added a comment.

add: zmmul arch string test


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103313/new/

https://reviews.llvm.org/D103313

Files:
  clang/lib/Basic/Targets/RISCV.cpp
  clang/lib/Basic/Targets/RISCV.h
  clang/lib/Driver/ToolChains/Arch/RISCV.cpp
  clang/test/Driver/riscv-arch.c
  llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
  llvm/lib/Target/RISCV/RISCV.td
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfoM.td
  llvm/lib/Target/RISCV/RISCVSubtarget.h
  llvm/test/CodeGen/RISCV/zmmul.ll
  llvm/test/MC/RISCV/rv32i-invalid.s
  llvm/test/MC/RISCV/rv32zmmul-invaild.s
  llvm/test/MC/RISCV/rv32zmmul-valid.s
  llvm/test/MC/RISCV/rv64zmmul-invalid.s
  llvm/test/MC/RISCV/rv64zmmul-valid.s

Index: llvm/test/MC/RISCV/rv64zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-valid.s
@@ -0,0 +1,5 @@
+# RUN: llvm-mc %s -triple=riscv64 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mulw ra, sp, gp
+mulw ra, sp, gp
Index: llvm/test/MC/RISCV/rv64zmmul-invalid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-invalid.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv64 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divw tp, t0, t1
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divuw t2, s0, s2
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remw a0, a1, a2
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remuw a3, a4, a5
Index: llvm/test/MC/RISCV/rv32zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-valid.s
@@ -0,0 +1,14 @@
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mul a4, ra, s0
+mul a4, ra, s0
+
+# CHECK-INST: mulh ra, zero, zero
+mulh x1, x0, x0
+
+# CHECK-INST: mulhsu t0, t2, t1
+mulhsu t0, t2, t1
+
+# CHECK-INST: mulhu a5, a4, a3
+mulhu a5, a4, a3
Index: llvm/test/MC/RISCV/rv32zmmul-invaild.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-invaild.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv32 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div s0, s0, s0
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divu gp, a0, a1
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+rem s2, s2, s8
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remu x18, x18, x24
Index: llvm/test/MC/RISCV/rv32i-invalid.s
===
--- llvm/test/MC/RISCV/rv32i-invalid.s
+++ llvm/test/MC/RISCV/rv32i-invalid.s
@@ -169,7 +169,7 @@
 xor s2, s2 # CHECK: :[[@LINE]]:1: error: too few operands for instruction
 
 # Instruction not in the base ISA
-mul a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
 amomaxu.w s5, s4, (s3) # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'A' (Atomic Instructions)
 fadd.s ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'F' (Single-Precision Floating-Point)
 fadd.h ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'Zfh' (Half-Precision Floating-Point)
Index: llvm/test/CodeGen/RISCV/zmmul.ll
===
--- /dev/null
+++ llvm/test/CodeGen/RISCV/zmmul.ll
@@ -0,0 +1,41 @@
+; RUN: llc -mtriple=riscv32 -mattr=+experimental-zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+experimental-zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv32 -mattr=+experimental-zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+; RUN: llc -mtriple=riscv64 -mattr=+experimental-zmmul -verify-machineinstrs < %s \
+; RUN:  | not 

[PATCH] D103313: [RISCV][Clang] Implement support for zmmul-experimental

2021-05-28 Thread ksyx via Phabricator via cfe-commits
ksyx added inline comments.



Comment at: llvm/lib/Target/RISCV/RISCVSubtarget.h:38
   bool HasStdExtM = false;
+  bool HasStdExtZmmul = false;
   bool HasStdExtA = false;

ksyx wrote:
> jrtc27 wrote:
> > Ditto
> The pattern here seems to be that Z* extensions is following its parent their 
> parent extensions, should we follow it? (See B and V)
But for Zfh it is not following HasStdExtF 樂 


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103313/new/

https://reviews.llvm.org/D103313

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


[PATCH] D103313: [RISCV][Clang] Implement support for zmmul-experimental

2021-05-28 Thread ksyx via Phabricator via cfe-commits
ksyx added a comment.

I have took a look into `ELFObjectFile.cpp` but I am not sure what work I need 
to do there since it seems other Z* extensions are not being handled there.




Comment at: llvm/lib/Target/RISCV/RISCVSubtarget.h:38
   bool HasStdExtM = false;
+  bool HasStdExtZmmul = false;
   bool HasStdExtA = false;

jrtc27 wrote:
> Ditto
The pattern here seems to be that Z* extensions is following its parent their 
parent extensions, should we follow it? (See B and V)


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103313/new/

https://reviews.llvm.org/D103313

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


[PATCH] D103313: [RISCV][Clang] Implement support for zmmul-experimental

2021-05-28 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 348608.
ksyx marked an inline comment as done.
ksyx added a comment.

- Move down zmmul lines to fit the ordering
- Add Zmmul into parseDirectiveAttribute


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103313/new/

https://reviews.llvm.org/D103313

Files:
  clang/lib/Basic/Targets/RISCV.cpp
  clang/lib/Basic/Targets/RISCV.h
  clang/lib/Driver/ToolChains/Arch/RISCV.cpp
  llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
  llvm/lib/Target/RISCV/RISCV.td
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfoM.td
  llvm/lib/Target/RISCV/RISCVSubtarget.h
  llvm/test/CodeGen/RISCV/zmmul.ll
  llvm/test/MC/RISCV/rv32i-invalid.s
  llvm/test/MC/RISCV/rv32zmmul-invaild.s
  llvm/test/MC/RISCV/rv32zmmul-valid.s
  llvm/test/MC/RISCV/rv64zmmul-invalid.s
  llvm/test/MC/RISCV/rv64zmmul-valid.s

Index: llvm/test/MC/RISCV/rv64zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-valid.s
@@ -0,0 +1,5 @@
+# RUN: llvm-mc %s -triple=riscv64 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mulw ra, sp, gp
+mulw ra, sp, gp
Index: llvm/test/MC/RISCV/rv64zmmul-invalid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-invalid.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv64 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divw tp, t0, t1
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divuw t2, s0, s2
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remw a0, a1, a2
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remuw a3, a4, a5
Index: llvm/test/MC/RISCV/rv32zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-valid.s
@@ -0,0 +1,14 @@
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mul a4, ra, s0
+mul a4, ra, s0
+
+# CHECK-INST: mulh ra, zero, zero
+mulh x1, x0, x0
+
+# CHECK-INST: mulhsu t0, t2, t1
+mulhsu t0, t2, t1
+
+# CHECK-INST: mulhu a5, a4, a3
+mulhu a5, a4, a3
Index: llvm/test/MC/RISCV/rv32zmmul-invaild.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-invaild.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv32 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div s0, s0, s0
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divu gp, a0, a1
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+rem s2, s2, s8
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remu x18, x18, x24
Index: llvm/test/MC/RISCV/rv32i-invalid.s
===
--- llvm/test/MC/RISCV/rv32i-invalid.s
+++ llvm/test/MC/RISCV/rv32i-invalid.s
@@ -169,7 +169,7 @@
 xor s2, s2 # CHECK: :[[@LINE]]:1: error: too few operands for instruction
 
 # Instruction not in the base ISA
-mul a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
 amomaxu.w s5, s4, (s3) # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'A' (Atomic Instructions)
 fadd.s ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'F' (Single-Precision Floating-Point)
 fadd.h ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'Zfh' (Half-Precision Floating-Point)
Index: llvm/test/CodeGen/RISCV/zmmul.ll
===
--- /dev/null
+++ llvm/test/CodeGen/RISCV/zmmul.ll
@@ -0,0 +1,41 @@
+; RUN: llc -mtriple=riscv32 -mattr=+experimental-zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+experimental-zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv32 -mattr=+experimental-zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+; RUN: llc -mtriple=riscv64 

[PATCH] D102839: [RISCV][Clang] Add -mno-div option to disable hardware int division

2021-05-28 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 348525.
ksyx added a comment.

split `Zmmul` implementation and `-mno-div` implementation


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102839/new/

https://reviews.llvm.org/D102839

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Arch/RISCV.cpp
  clang/test/Driver/riscv-no-div.c


Index: clang/test/Driver/riscv-no-div.c
===
--- /dev/null
+++ clang/test/Driver/riscv-no-div.c
@@ -0,0 +1,30 @@
+// RUN: %clang -target riscv32-unknown-elf %s -mno-div -S -o - 2>&1 \
+// RUN:   | not FileCheck -check-prefix=CHECK-DIV %s
+
+// RUN: %clang -target riscv64-unknown-elf %s -mno-div -S -o - 2>&1 \
+// RUN:   | not FileCheck -check-prefix=CHECK-DIV %s
+
+// RUN: %clang -target riscv32-unknown-elf %s -mno-div -S -o - 2>&1 \
+// RUN:   | not FileCheck -check-prefix=CHECK-REM %s
+
+// RUN: %clang -target riscv64-unknown-elf %s -mno-div -S -o - 2>&1 \
+// RUN:   | not FileCheck -check-prefix=CHECK-REM %s
+
+// RUN: %clang -target riscv32-unknown-elf %s -mno-div -S -o - 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-MUL %s
+
+// RUN: %clang -target riscv64-unknown-elf %s -mno-div -S -o - 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-MUL %s
+
+// RUN: %clang -target riscv32-unknown-elf %s -S -o - 2>&1 \
+// RUN:   | FileCheck -check-prefixes=CHECK-MUL,CHECK-DIV,CHECK-REM %s
+
+// RUN: %clang -target riscv64-unknown-elf %s -S -o - 2>&1 \
+// RUN:   | FileCheck -check-prefixes=CHECK-MUL,CHECK-DIV,CHECK-REM %s
+
+int foo(int x, int y) {
+  // CHECK-DIV: div{{w?}} a{{[0-9]}}, a{{[0-9]}}, a{{[0-9]}}
+  // CHECK-REM: rem{{w?}} a{{[0-9]}}, a{{[0-9]}}, a{{[0-9]}}
+  // CHECK-MUL: mul{{w?}} a{{[0-9]}}, a{{[0-9]}}, a{{[0-9]}}
+  return (x / y) * (x % y);
+}
Index: clang/lib/Driver/ToolChains/Arch/RISCV.cpp
===
--- clang/lib/Driver/ToolChains/Arch/RISCV.cpp
+++ clang/lib/Driver/ToolChains/Arch/RISCV.cpp
@@ -572,6 +572,15 @@
   // Now add any that the user explicitly requested on the command line,
   // which may override the defaults.
   handleTargetFeaturesGroup(Args, Features, 
options::OPT_m_riscv_Features_Group);
+
+  bool noDiv = Args.hasFlag(options::OPT_mno_div, options::OPT_mdiv, false);
+  for (auto  : Features) {
+if (noDiv && (Feature == "+m" || Feature == "-div")) {
+  Feature = "+experimental-zmmul";
+} else if (Feature == "+div") {
+  Feature = "+m";
+}
+  }
 }
 
 StringRef riscv::getRISCVABI(const ArgList , const llvm::Triple ) {
Index: clang/include/clang/Driver/Options.td
===
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -3146,6 +3146,10 @@
   HelpText<"Enable using library calls for save and restore">;
 def mno_save_restore : Flag<["-"], "mno-save-restore">, 
Group,
   HelpText<"Disable using library calls for save and restore">;
+def mno_div : Flag<["-"], "mno-div">, Group,
+  HelpText<"Disable integer division instructions in M extension">;
+def mdiv : Flag<["-"], "mdiv">, Group,
+  HelpText<"Enable integer division instructions in M extension">;
 def mcmodel_EQ_medlow : Flag<["-"], "mcmodel=medlow">, 
Group,
   Flags<[CC1Option]>, Alias, AliasArgs<["small"]>,
   HelpText<"Equivalent to -mcmodel=small, compatible with RISC-V gcc.">;


Index: clang/test/Driver/riscv-no-div.c
===
--- /dev/null
+++ clang/test/Driver/riscv-no-div.c
@@ -0,0 +1,30 @@
+// RUN: %clang -target riscv32-unknown-elf %s -mno-div -S -o - 2>&1 \
+// RUN:   | not FileCheck -check-prefix=CHECK-DIV %s
+
+// RUN: %clang -target riscv64-unknown-elf %s -mno-div -S -o - 2>&1 \
+// RUN:   | not FileCheck -check-prefix=CHECK-DIV %s
+
+// RUN: %clang -target riscv32-unknown-elf %s -mno-div -S -o - 2>&1 \
+// RUN:   | not FileCheck -check-prefix=CHECK-REM %s
+
+// RUN: %clang -target riscv64-unknown-elf %s -mno-div -S -o - 2>&1 \
+// RUN:   | not FileCheck -check-prefix=CHECK-REM %s
+
+// RUN: %clang -target riscv32-unknown-elf %s -mno-div -S -o - 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-MUL %s
+
+// RUN: %clang -target riscv64-unknown-elf %s -mno-div -S -o - 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-MUL %s
+
+// RUN: %clang -target riscv32-unknown-elf %s -S -o - 2>&1 \
+// RUN:   | FileCheck -check-prefixes=CHECK-MUL,CHECK-DIV,CHECK-REM %s
+
+// RUN: %clang -target riscv64-unknown-elf %s -S -o - 2>&1 \
+// RUN:   | FileCheck -check-prefixes=CHECK-MUL,CHECK-DIV,CHECK-REM %s
+
+int foo(int x, int y) {
+  // CHECK-DIV: div{{w?}} a{{[0-9]}}, a{{[0-9]}}, a{{[0-9]}}
+  // CHECK-REM: rem{{w?}} a{{[0-9]}}, a{{[0-9]}}, a{{[0-9]}}
+  // CHECK-MUL: mul{{w?}} a{{[0-9]}}, a{{[0-9]}}, a{{[0-9]}}
+  return (x / y) * (x % y);
+}
Index: clang/lib/Driver/ToolChains/Arch/RISCV.cpp
===
--- 

[PATCH] D103313: [RISCV][Clang] Implement support for zmmul-experimental

2021-05-28 Thread ksyx via Phabricator via cfe-commits
ksyx created this revision.
ksyx added reviewers: asb, craig.topper, luismarques, jrtc27, kito-cheng, 
rsmith, shiva0217.
Herald added subscribers: vkmr, frasercrmck, evandro, apazos, sameer.abuasal, 
s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, 
rogfer01, edward-jones, zzheng, niosHD, sabuasal, simoncook, johnrusso, rbar, 
hiraditya.
ksyx requested review of this revision.
Herald added subscribers: llvm-commits, cfe-commits, MaskRay.
Herald added projects: clang, LLVM.

This patch implements recently merged extension Zmmul 
, a subextension of M 
(Integer Multiplication and Division) consisting only multiplication part of it.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D103313

Files:
  clang/lib/Basic/Targets/RISCV.cpp
  clang/lib/Basic/Targets/RISCV.h
  clang/lib/Driver/ToolChains/Arch/RISCV.cpp
  llvm/lib/Target/RISCV/RISCV.td
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfoM.td
  llvm/lib/Target/RISCV/RISCVSubtarget.h
  llvm/test/CodeGen/RISCV/zmmul.ll
  llvm/test/MC/RISCV/rv32i-invalid.s
  llvm/test/MC/RISCV/rv32zmmul-invaild.s
  llvm/test/MC/RISCV/rv32zmmul-valid.s
  llvm/test/MC/RISCV/rv64zmmul-invalid.s
  llvm/test/MC/RISCV/rv64zmmul-valid.s

Index: llvm/test/MC/RISCV/rv64zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-valid.s
@@ -0,0 +1,5 @@
+# RUN: llvm-mc %s -triple=riscv64 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mulw ra, sp, gp
+mulw ra, sp, gp
Index: llvm/test/MC/RISCV/rv64zmmul-invalid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-invalid.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv64 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divw tp, t0, t1
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divuw t2, s0, s2
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remw a0, a1, a2
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remuw a3, a4, a5
Index: llvm/test/MC/RISCV/rv32zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-valid.s
@@ -0,0 +1,14 @@
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mul a4, ra, s0
+mul a4, ra, s0
+
+# CHECK-INST: mulh ra, zero, zero
+mulh x1, x0, x0
+
+# CHECK-INST: mulhsu t0, t2, t1
+mulhsu t0, t2, t1
+
+# CHECK-INST: mulhu a5, a4, a3
+mulhu a5, a4, a3
Index: llvm/test/MC/RISCV/rv32zmmul-invaild.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-invaild.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv32 -mattr=+experimental-zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div s0, s0, s0
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divu gp, a0, a1
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+rem s2, s2, s8
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remu x18, x18, x24
Index: llvm/test/MC/RISCV/rv32i-invalid.s
===
--- llvm/test/MC/RISCV/rv32i-invalid.s
+++ llvm/test/MC/RISCV/rv32i-invalid.s
@@ -169,7 +169,7 @@
 xor s2, s2 # CHECK: :[[@LINE]]:1: error: too few operands for instruction
 
 # Instruction not in the base ISA
-mul a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
 amomaxu.w s5, s4, (s3) # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'A' (Atomic Instructions)
 fadd.s ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'F' (Single-Precision Floating-Point)
 fadd.h ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'Zfh' (Half-Precision Floating-Point)
Index: llvm/test/CodeGen/RISCV/zmmul.ll
===
--- /dev/null
+++ llvm/test/CodeGen/RISCV/zmmul.ll
@@ -0,0 +1,41 @@

[PATCH] D102839: [RISCV][Clang] Add -mno-div option to disable hardware int division

2021-05-27 Thread ksyx via Phabricator via cfe-commits
ksyx added a comment.

In D102839#2784297 , @luismarques 
wrote:

> In D102839#2784275 , @ksyx wrote:
>
>> Thanks for mentioning that! Now, I changed the effect of `no-div` option 
>> into choosing a proper extension and implemented the `Zmmul` subextension. 
>> Is this solution acceptable or are there anything need further changes?
>
> It seems like the community is quickly converging on just using the ISA 
> string with Zmmul, and not using no-div. While being compatible with the GNU 
> tools is nice, if they are planning on dropping support for no-div soonish 
> then we probably shouldn't add support for it. IMO, Zmmul should be a 
> separate patch and presumably (at the moment) be gated by 
> `-menable-experimental-extensions`.

Thanks for the news and I think having experimental-zmmul a separate patch is a 
good idea. But which implementation of `no-div` is better, the one that chooses 
extension between M and zmmul or the one that simply being an attribute like 
Diff 3, or are there any other suggestions?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102839/new/

https://reviews.llvm.org/D102839

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


[PATCH] D102839: [RISCV][Clang] Add -mno-div option to disable hardware int division

2021-05-27 Thread ksyx via Phabricator via cfe-commits
ksyx added a comment.

In D102839#2784197 , @luismarques 
wrote:

> In D102839#2783636 , @ksyx wrote:
>
>> So it seems the better way to do this would definitely by adding a 
>> subextension as the spec had changed. But I'd like  also to ask how will GCC 
>> deal with this option, and should we make this option an alias to turn off M 
>> extension and turn on ZMMul extension?
>
> Regarding "turn off M extension", see Krste's comment in the PR:
>
>> I think -mno-div is OK as a compiler option, but it's meaning is for the 
>> compiler not to generate divide instructions, not to indicate what the ISA 
>> is. So for example, -mno-div should not set ELF flags in binary to indicate 
>> ISA doesn't have divide - it should simply not generate divide instructions.

Thanks for mentioning that! Now, I changed the effect of `no-div` option into 
choosing a proper extension and implemented the `Zmmul` subextension. Is this 
solution acceptable or are there anything need further changes?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102839/new/

https://reviews.llvm.org/D102839

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


[PATCH] D102839: [RISCV][Clang] Add -mno-div option to disable hardware int division

2021-05-27 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 348206.
ksyx marked an inline comment as done.
ksyx edited the summary of this revision.
ksyx added a comment.

Implemented Zmmul subextension, and let `no-div` to be using this subextension 
instead of M itself.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102839/new/

https://reviews.llvm.org/D102839

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Basic/Targets/RISCV.cpp
  clang/lib/Basic/Targets/RISCV.h
  clang/lib/Driver/ToolChains/Arch/RISCV.cpp
  clang/test/Driver/riscv-no-div.c
  llvm/lib/Target/RISCV/RISCV.td
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfoM.td
  llvm/lib/Target/RISCV/RISCVSubtarget.h
  llvm/test/CodeGen/RISCV/zmmul.ll
  llvm/test/MC/RISCV/rv32i-invalid.s
  llvm/test/MC/RISCV/rv32zmmul-invaild.s
  llvm/test/MC/RISCV/rv32zmmul-valid.s
  llvm/test/MC/RISCV/rv64zmmul-invalid.s
  llvm/test/MC/RISCV/rv64zmmul-valid.s

Index: llvm/test/MC/RISCV/rv64zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-valid.s
@@ -0,0 +1,5 @@
+# RUN: llvm-mc %s -triple=riscv64 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mulw ra, sp, gp
+mulw ra, sp, gp
Index: llvm/test/MC/RISCV/rv64zmmul-invalid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64zmmul-invalid.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv64 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divw tp, t0, t1
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divuw t2, s0, s2
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remw a0, a1, a2
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remuw a3, a4, a5
Index: llvm/test/MC/RISCV/rv32zmmul-valid.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-valid.s
@@ -0,0 +1,14 @@
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mul a4, ra, s0
+mul a4, ra, s0
+
+# CHECK-INST: mulh ra, zero, zero
+mulh x1, x0, x0
+
+# CHECK-INST: mulhsu t0, t2, t1
+mulhsu t0, t2, t1
+
+# CHECK-INST: mulhu a5, a4, a3
+mulhu a5, a4, a3
Index: llvm/test/MC/RISCV/rv32zmmul-invaild.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32zmmul-invaild.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv32 -mattr=+zmmul -riscv-no-aliases 2>&1 \
+# RUN:  | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div s0, s0, s0
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+divu gp, a0, a1
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+rem s2, s2, s8
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+remu x18, x18, x24
Index: llvm/test/MC/RISCV/rv32i-invalid.s
===
--- llvm/test/MC/RISCV/rv32i-invalid.s
+++ llvm/test/MC/RISCV/rv32i-invalid.s
@@ -169,7 +169,7 @@
 xor s2, s2 # CHECK: :[[@LINE]]:1: error: too few operands for instruction
 
 # Instruction not in the base ISA
-mul a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
+div a4, ra, s0 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'M' (Integer Multiplication and Division)
 amomaxu.w s5, s4, (s3) # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'A' (Atomic Instructions)
 fadd.s ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'F' (Single-Precision Floating-Point)
 fadd.h ft0, ft1, ft2 # CHECK: :[[@LINE]]:1: error: instruction requires the following: 'Zfh' (Half-Precision Floating-Point)
Index: llvm/test/CodeGen/RISCV/zmmul.ll
===
--- /dev/null
+++ llvm/test/CodeGen/RISCV/zmmul.ll
@@ -0,0 +1,41 @@
+; RUN: llc -mtriple=riscv32 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv32 -mattr=+zmmul -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+; RUN: llc -mtriple=riscv64 -mattr=+zmmul 

[PATCH] D102839: [RISCV][Clang] Add -mno-div option to disable hardware int division

2021-05-26 Thread ksyx via Phabricator via cfe-commits
ksyx added a comment.

So it seems the better way to do this would definitely by adding a subextension 
as the spec had changed. But I'd like  also to ask how will GCC deal with this 
option, and should we make this option an alias to turn off M extension and 
turn on ZMMul extension?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102839/new/

https://reviews.llvm.org/D102839

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


[PATCH] D102839: [RISCV][Clang] Add -mno-div option to disable hardware int division

2021-05-25 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 347637.
ksyx added a comment.

fix: indent


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102839/new/

https://reviews.llvm.org/D102839

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Basic/Targets/RISCV.cpp
  clang/lib/Basic/Targets/RISCV.h
  clang/lib/Driver/ToolChains/Arch/RISCV.cpp
  clang/test/Driver/riscv-no-div.c
  llvm/lib/Target/RISCV/RISCV.td
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfoM.td
  llvm/lib/Target/RISCV/RISCVSubtarget.h
  llvm/test/CodeGen/RISCV/no-div.ll
  llvm/test/MC/RISCV/rv32m-no-div-div.s
  llvm/test/MC/RISCV/rv32m-no-div-mul.s
  llvm/test/MC/RISCV/rv64m-no-div-div.s
  llvm/test/MC/RISCV/rv64m-no-div-mul.s

Index: llvm/test/MC/RISCV/rv64m-no-div-mul.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64m-no-div-mul.s
@@ -0,0 +1,5 @@
+# RUN: llvm-mc %s -triple=riscv64 -mattr=+m,+no-div -riscv-no-aliases 2>&1 \
+# RUN: | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mulw ra, sp, gp
+mulw ra, sp, gp
Index: llvm/test/MC/RISCV/rv64m-no-div-div.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64m-no-div-div.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv64 -mattr=+m,+no-div -riscv-no-aliases 2>&1 \
+# RUN: | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: Hardware integral division not disabled
+divw tp, t0, t1
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: Hardware integral division not disabled
+divuw t2, s0, s2
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: Hardware integral division not disabled
+remw a0, a1, a2
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: Hardware integral division not disabled
+remuw a3, a4, a5
Index: llvm/test/MC/RISCV/rv32m-no-div-mul.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32m-no-div-mul.s
@@ -0,0 +1,14 @@
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+m,+no-div -riscv-no-aliases 2>&1 \
+# RUN: | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mul a4, ra, s0
+mul a4, ra, s0
+
+# CHECK-INST: mulh ra, zero, zero
+mulh x1, x0, x0
+
+# CHECK-INST: mulhsu t0, t2, t1
+mulhsu t0, t2, t1
+
+# CHECK-INST: mulhu a5, a4, a3
+mulhu a5, a4, a3
Index: llvm/test/MC/RISCV/rv32m-no-div-div.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32m-no-div-div.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv32 -mattr=+m,+no-div -riscv-no-aliases 2>&1 \
+# RUN: | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: Hardware integral division not disabled
+div s0, s0, s0
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: Hardware integral division not disabled
+divu gp, a0, a1
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: Hardware integral division not disabled
+rem s2, s2, s8
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: Hardware integral division not disabled
+remu x18, x18, x24
Index: llvm/test/CodeGen/RISCV/no-div.ll
===
--- /dev/null
+++ llvm/test/CodeGen/RISCV/no-div.ll
@@ -0,0 +1,45 @@
+; RUN: llc -mtriple=riscv32 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv32 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+; RUN: llc -mtriple=riscv64 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+
+; RUN: llc -mtriple=riscv32 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-UDIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-UDIV %s
+; RUN: llc -mtriple=riscv32 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-UREM %s
+; RUN: llc -mtriple=riscv64 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-UREM %s
+
+; RUN: llc -mtriple=riscv32 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | FileCheck -check-prefix=CHECK-MUL %s
+; RUN: llc -mtriple=riscv64 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | FileCheck -check-prefix=CHECK-MUL %s
+
+; RUN: llc -mtriple=riscv32 -mattr=+m -verify-machineinstrs < %s \
+; RUN:  | FileCheck -check-prefixes=CHECK-MUL,CHECK-UDIV,CHECK-DIV,CHECK-UREM,CHECK-REM %s
+; RUN: llc -mtriple=riscv64 -mattr=+m -verify-machineinstrs < %s \
+; RUN:  | FileCheck 

[PATCH] D102839: [RISCV][Clang] Add -mno-div option to disable hardware int division

2021-05-25 Thread ksyx via Phabricator via cfe-commits
ksyx updated this revision to Diff 347603.
ksyx marked 2 inline comments as done.
ksyx retitled this revision from "[RISCV][Clang] Add -mno-idiv option to 
disable hardware int division" to "[RISCV][Clang] Add -mno-div option to 
disable hardware int division".
ksyx edited the summary of this revision.
ksyx added a reviewer: craig.topper.
ksyx added a comment.

- remove redundant macro definition
- split mul and div handling in `RISCVISelLowering.cpp`
- rename `no-idiv` into `no-div`


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102839/new/

https://reviews.llvm.org/D102839

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Basic/Targets/RISCV.cpp
  clang/lib/Basic/Targets/RISCV.h
  clang/lib/Driver/ToolChains/Arch/RISCV.cpp
  clang/test/Driver/riscv-no-div.c
  llvm/lib/Target/RISCV/RISCV.td
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfoM.td
  llvm/lib/Target/RISCV/RISCVSubtarget.h
  llvm/test/CodeGen/RISCV/no-div.ll
  llvm/test/MC/RISCV/rv32m-no-div-div.s
  llvm/test/MC/RISCV/rv32m-no-div-mul.s
  llvm/test/MC/RISCV/rv64m-no-div-div.s
  llvm/test/MC/RISCV/rv64m-no-div-mul.s

Index: llvm/test/MC/RISCV/rv64m-no-div-mul.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64m-no-div-mul.s
@@ -0,0 +1,5 @@
+# RUN: llvm-mc %s -triple=riscv64 -mattr=+m,+no-div -riscv-no-aliases 2>&1 \
+# RUN: | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mulw ra, sp, gp
+mulw ra, sp, gp
Index: llvm/test/MC/RISCV/rv64m-no-div-div.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64m-no-div-div.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv64 -mattr=+m,+no-div -riscv-no-aliases 2>&1 \
+# RUN: | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: Hardware integral division not disabled
+divw tp, t0, t1
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: Hardware integral division not disabled
+divuw t2, s0, s2
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: Hardware integral division not disabled
+remw a0, a1, a2
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: Hardware integral division not disabled
+remuw a3, a4, a5
Index: llvm/test/MC/RISCV/rv32m-no-div-mul.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32m-no-div-mul.s
@@ -0,0 +1,14 @@
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+m,+no-div -riscv-no-aliases 2>&1 \
+# RUN: | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mul a4, ra, s0
+mul a4, ra, s0
+
+# CHECK-INST: mulh ra, zero, zero
+mulh x1, x0, x0
+
+# CHECK-INST: mulhsu t0, t2, t1
+mulhsu t0, t2, t1
+
+# CHECK-INST: mulhu a5, a4, a3
+mulhu a5, a4, a3
Index: llvm/test/MC/RISCV/rv32m-no-div-div.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32m-no-div-div.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv32 -mattr=+m,+no-div -riscv-no-aliases 2>&1 \
+# RUN: | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: Hardware integral division not disabled
+div s0, s0, s0
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: Hardware integral division not disabled
+divu gp, a0, a1
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: Hardware integral division not disabled
+rem s2, s2, s8
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: Hardware integral division not disabled
+remu x18, x18, x24
Index: llvm/test/CodeGen/RISCV/no-div.ll
===
--- /dev/null
+++ llvm/test/CodeGen/RISCV/no-div.ll
@@ -0,0 +1,45 @@
+; RUN: llc -mtriple=riscv32 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv32 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+; RUN: llc -mtriple=riscv64 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+
+; RUN: llc -mtriple=riscv32 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-UDIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-UDIV %s
+; RUN: llc -mtriple=riscv32 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-UREM %s
+; RUN: llc -mtriple=riscv64 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-UREM %s
+
+; RUN: llc -mtriple=riscv32 -mattr=+m,+no-div -verify-machineinstrs < %s \
+; RUN:  | FileCheck 

[PATCH] D102839: [RISCV][Clang] Add -mno-idiv option to disable hardware int division

2021-05-22 Thread ksyx via Phabricator via cfe-commits
ksyx added a comment.

In D102839#2775732 , @craig.topper 
wrote:

> Isn't the gcc name for this -mno-div? Should we be consistent?

I am not sure whether using `-mno-div` might bring in some confusion since from 
the name `no-div` itself it expresses a sense of excluding all hardware 
division instructions, both float ones and integral ones, from my personal 
understanding. However, as in the documentation 
, it only affects the 
integral ones and float one was controlled by another. Thus for the clearance I 
used this naming.

I have not much understanding about what compatibility with GCC we need to 
achieve but I am okay with both kinds of naming and changing this is totally ok 
if required.




Comment at: clang/lib/Basic/Targets/RISCV.cpp:154
+  if (DisableHardwareIntDiv) {
+Builder.defineMacro("__riscv_no_idiv");
   }

craig.topper wrote:
> Does gcc also have this define? Why do we need this in addition to not 
> defining __riscv_div. 
Ok. I will remove it.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102839/new/

https://reviews.llvm.org/D102839

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


[PATCH] D102839: [RISCV][Clang] Add -mno-idiv option to disable hardware int division

2021-05-20 Thread ksyx via Phabricator via cfe-commits
ksyx created this revision.
ksyx added reviewers: asb, rsmith, shiva0217.
Herald added subscribers: vkmr, frasercrmck, dang, evandro, luismarques, 
apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, 
brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, kito-cheng, 
niosHD, sabuasal, simoncook, johnrusso, rbar, hiraditya.
ksyx requested review of this revision.
Herald added subscribers: llvm-commits, cfe-commits, MaskRay.
Herald added projects: clang, LLVM.

With option `-mno-idiv`, hardware integral division and modulus instructions in 
M extension will be disabled, while leaving multiply instructions enabled.

I am not sure whether there is a better way of implementing this, so please 
point out if there is one, thanks.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102839

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Basic/Targets/RISCV.cpp
  clang/lib/Basic/Targets/RISCV.h
  clang/lib/Driver/ToolChains/Arch/RISCV.cpp
  clang/test/Driver/riscv-no-idiv.c
  llvm/lib/Target/RISCV/RISCV.td
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfoM.td
  llvm/lib/Target/RISCV/RISCVSubtarget.h
  llvm/test/CodeGen/RISCV/no-idiv.ll
  llvm/test/MC/RISCV/rv32m-no-idiv-div.s
  llvm/test/MC/RISCV/rv32m-no-idiv-mul.s
  llvm/test/MC/RISCV/rv64m-no-idiv-div.s
  llvm/test/MC/RISCV/rv64m-no-idiv-mul.s

Index: llvm/test/MC/RISCV/rv64m-no-idiv-mul.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64m-no-idiv-mul.s
@@ -0,0 +1,5 @@
+# RUN: llvm-mc %s -triple=riscv64 -mattr=+m,+no-idiv -riscv-no-aliases 2>&1 \
+# RUN: | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mulw ra, sp, gp
+mulw ra, sp, gp
Index: llvm/test/MC/RISCV/rv64m-no-idiv-div.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv64m-no-idiv-div.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv64 -mattr=+m,+no-idiv -riscv-no-aliases 2>&1 \
+# RUN: | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: Hardware integral division not disabled
+divw tp, t0, t1
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: Hardware integral division not disabled
+divuw t2, s0, s2
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: Hardware integral division not disabled
+remw a0, a1, a2
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: Hardware integral division not disabled
+remuw a3, a4, a5
Index: llvm/test/MC/RISCV/rv32m-no-idiv-mul.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32m-no-idiv-mul.s
@@ -0,0 +1,14 @@
+# RUN: llvm-mc %s -triple=riscv32 -mattr=+m,+no-idiv -riscv-no-aliases 2>&1 \
+# RUN: | FileCheck -check-prefixes=CHECK-INST %s
+
+# CHECK-INST: mul a4, ra, s0
+mul a4, ra, s0
+
+# CHECK-INST: mulh ra, zero, zero
+mulh x1, x0, x0
+
+# CHECK-INST: mulhsu t0, t2, t1
+mulhsu t0, t2, t1
+
+# CHECK-INST: mulhu a5, a4, a3
+mulhu a5, a4, a3
Index: llvm/test/MC/RISCV/rv32m-no-idiv-div.s
===
--- /dev/null
+++ llvm/test/MC/RISCV/rv32m-no-idiv-div.s
@@ -0,0 +1,14 @@
+# RUN: not llvm-mc %s -triple=riscv32 -mattr=+m,+no-idiv -riscv-no-aliases 2>&1 \
+# RUN: | FileCheck -check-prefixes=CHECK-ERROR %s
+
+# CHECK-ERROR: 5:1: error: instruction requires the following: Hardware integral division not disabled
+div s0, s0, s0
+
+# CHECK-ERROR: 8:1: error: instruction requires the following: Hardware integral division not disabled
+divu gp, a0, a1
+
+# CHECK-ERROR: 11:1: error: instruction requires the following: Hardware integral division not disabled
+rem s2, s2, s8
+
+# CHECK-ERROR: 14:1: error: instruction requires the following: Hardware integral division not disabled
+remu x18, x18, x24
Index: llvm/test/CodeGen/RISCV/no-idiv.ll
===
--- /dev/null
+++ llvm/test/CodeGen/RISCV/no-idiv.ll
@@ -0,0 +1,45 @@
+; RUN: llc -mtriple=riscv32 -mattr=+m,+no-idiv -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+m,+no-idiv -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-DIV %s
+; RUN: llc -mtriple=riscv32 -mattr=+m,+no-idiv -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+; RUN: llc -mtriple=riscv64 -mattr=+m,+no-idiv -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-REM %s
+
+; RUN: llc -mtriple=riscv32 -mattr=+m,+no-idiv -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-UDIV %s
+; RUN: llc -mtriple=riscv64 -mattr=+m,+no-idiv -verify-machineinstrs < %s \
+; RUN:  | not FileCheck -check-prefix=CHECK-UDIV %s
+; RUN: llc -mtriple=riscv32 -mattr=+m,+no-idiv -verify-machineinstrs < %s \
+; RUN:  |