Skip to content

Commit df87fa6

Browse files
committed
Auto merge of #164833 - JonathanBrouwer:rollup-dUtDpgI, r=JonathanBrouwer
Rollup of 2 pull requests Successful merges: - #164800 (Use LLVMCreateBuilderForModule to create IR builders) - #164815 (rustfmt: prevent more comment loss in `cfg_select!`)
2 parents fbcff0a + a1b7d6e commit df87fa6

8 files changed

Lines changed: 73 additions & 6 deletions

File tree

‎compiler/rustc_codegen_llvm/src/builder.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl<'a, 'll> SBuilder<'a, 'll> {
9494
impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
9595
fn with_cx(scx: &'a GenericCx<'ll, CX>) -> Self {
9696
// Create a fresh builder from the simple context.
97-
let llbuilder = unsafe { llvm::LLVMCreateBuilderInContext(scx.deref().borrow().llcx) };
97+
let llbuilder = unsafe { llvm::LLVMCreateBuilderForModule(scx.deref().borrow().llmod) };
9898
GenericBuilder { llbuilder, cx: scx, span: rustc_span::DUMMY_SP }
9999
}
100100

‎compiler/rustc_codegen_llvm/src/llvm/ffi.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ unsafe extern "C" {
11961196
);
11971197

11981198
// Instruction builders
1199-
pub(crate) fn LLVMCreateBuilderInContext(C: &Context) -> &mut Builder<'_>;
1199+
pub(crate) fn LLVMCreateBuilderForModule(M: &Module) -> &mut Builder<'_>;
12001200
pub(crate) fn LLVMPositionBuilderAtEnd<'a>(Builder: &Builder<'a>, Block: &'a BasicBlock);
12011201
pub(crate) fn LLVMGetInsertBlock<'a>(Builder: &Builder<'a>) -> &'a BasicBlock;
12021202
pub(crate) fn LLVMDisposeBuilder<'a>(Builder: &'a mut Builder<'a>);

‎compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,12 @@ extern "C" LLVMValueRef LLVMRustBuildMemSet(LLVMBuilderRef B, LLVMValueRef Dst,
14001400
MaybeAlign(DstAlign), IsVolatile));
14011401
}
14021402

1403+
#if LLVM_VERSION_LT(24, 0)
1404+
extern "C" LLVMBuilderRef LLVMCreateBuilderForModule(LLVMModuleRef M) {
1405+
return LLVMCreateBuilderInContext(LLVMGetModuleContext(M));
1406+
}
1407+
#endif
1408+
14031409
extern "C" void LLVMRustPositionBuilderPastAllocas(LLVMBuilderRef B,
14041410
LLVMValueRef Fn) {
14051411
Function *F = unwrap<Function>(Fn);

‎src/tools/rustfmt/src/macros.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@ fn format_cfg_select(
16301630
}
16311631
};
16321632

1633-
match arm.double_block_inner() {
1633+
match arm.double_block_inner(context) {
16341634
// FIXME(ytmimi) The formatting for the `$predicate => {{}}` case isn't very robust.
16351635
// There's the potential that we'll lose comments in between the predicate, the `=>`
16361636
// and the body. We also don't handle comments like `{/*comment*/{}/*comment*/}`.

‎src/tools/rustfmt/src/parse/macros/cfg_select.rs‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ use rustc_parse::parser::{AllowConstBlockItems, ForceCollect};
1313
use rustc_span::Span;
1414
use tracing::debug;
1515

16+
use crate::comment::contains_comment;
1617
use crate::expr::is_unsafe_block;
1718
use crate::parse::macros::build_stream_parser;
1819
use crate::parse::session::ParseSess;
20+
use crate::rewrite::RewriteContext;
1921
use crate::spanned::Spanned;
2022

2123
// NB: parser recovery can return `Ok(expr)` (or other non-terminals) and still emit errors. We need
@@ -164,7 +166,7 @@ impl CfgSelectArm {
164166
/// Is the cfg_select! arm written with two consecutive blocks.
165167
/// For example `$predicate => {{}}`. If so, extract the inner block.
166168
/// rustfmt will preserve the double block syntax.
167-
pub(crate) fn double_block_inner(&self) -> Option<&ast::Expr> {
169+
pub(crate) fn double_block_inner(&self, context: &RewriteContext<'_>) -> Option<&ast::Expr> {
168170
// In order to see if we can reach in and grab the inner block we'll
169171
// check the following properties of the outer block:
170172
// 1. It has a single statement that's also a block
@@ -180,7 +182,21 @@ impl CfgSelectArm {
180182
{
181183
match &b.stmts[0].kind {
182184
// This is where we check that the inner block's statement is also a block
183-
ast::StmtKind::Expr(e) if matches!(e.kind, ast::ExprKind::Block(..)) => Some(e),
185+
ast::StmtKind::Expr(e) if matches!(e.kind, ast::ExprKind::Block(..)) => {
186+
let pre_span = b.span.shrink_to_lo().between(e.span());
187+
let pre_snippet = context.snippet(pre_span).trim();
188+
189+
let post_span = e.span().shrink_to_hi().between(b.span.shrink_to_hi());
190+
let post_snippet = context.snippet(post_span).trim();
191+
192+
if !contains_comment(pre_snippet) && !contains_comment(post_snippet) {
193+
// To prevent comment loss between the blocks we only want to return
194+
// the inner block if there aren't any pre / post comments.
195+
Some(e)
196+
} else {
197+
None
198+
}
199+
}
184200
_ => None,
185201
}
186202
}

‎src/tools/rustfmt/tests/source/cfg_select_edge_cases.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ fn cfg_select_comments_that_might_get_lost() {
6161
// before closing brace
6262
}
6363
}
64+
cfg_select! {
65+
_ => { /* after opening brace */
66+
{ x }
67+
/* before closing brace */
68+
}
69+
}
6470
cfg_select! {
6571
_ => /* after => */ {{ x }}
6672
}

‎src/tools/rustfmt/tests/target/cfg_select_edge_cases.rs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,18 @@ fn check_match_block_formatting() {
7272

7373
fn cfg_select_comments_that_might_get_lost() {
7474
cfg_select! {
75-
_ => {{ x }}
75+
_ => {
76+
// after opening brace
77+
{ x }
78+
// before closing brace
79+
}
80+
}
81+
cfg_select! {
82+
_ => {
83+
/* after opening brace */
84+
{ x }
85+
/* before closing brace */
86+
}
7687
}
7788
cfg_select! {
7889
_ => {{ x }}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//@ rustfmt-unstable: true
2+
3+
fn gettid() -> u64 {
4+
cfg_select! {
5+
any(target_os = "android", target_os = "linux") => gettid_linux_like(),
6+
any(target_os = "nto", target_os = "qnx") => unsafe { libc::gettid() as u64 },
7+
target_os = "openbsd" => unsafe { libc::getthrid() as u64 },
8+
target_os = "freebsd" => unsafe { libc::pthread_getthreadid_np() as u64 },
9+
target_os = "netbsd" => unsafe { libc::_lwp_self() as u64 },
10+
any(target_os = "solaris", target_os = "illumos") => {
11+
// On Solaris and Illumos, the `pthread_t` is the OS TID.
12+
unsafe { libc::pthread_self() as u64 }
13+
}
14+
target_vendor = "apple" => {
15+
let mut id = 0u64;
16+
let status: libc::c_int = unsafe { libc::pthread_threadid_np(0, &mut id) };
17+
assert_eq!(status, 0);
18+
id
19+
}
20+
windows => {
21+
use windows_sys::Win32::System::Threading::GetCurrentThreadId;
22+
unsafe { GetCurrentThreadId() as u64 }
23+
}
24+
_ => {
25+
compile_error!("platform has no gettid")
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)