From 15973f1a55d1ba018ef7c974b8280402cf552bda Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 7 Jul 2024 08:39:08 +0200 Subject: [PATCH] Fix stop_watch on linux --- crates/ide/src/status.rs | 1 - crates/profile/src/stop_watch.rs | 7 +++---- xtask/src/codegen.rs | 10 ++++++---- xtask/src/codegen/assists_doc_tests.rs | 4 ++-- xtask/src/codegen/diagnostics_docs.rs | 2 +- xtask/src/codegen/grammar.rs | 6 +++--- xtask/src/codegen/lints.rs | 2 +- xtask/src/flags.rs | 14 +++++++++++++- 8 files changed, 29 insertions(+), 17 deletions(-) diff --git a/crates/ide/src/status.rs b/crates/ide/src/status.rs index 69526ddef9..b998c0bfc6 100644 --- a/crates/ide/src/status.rs +++ b/crates/ide/src/status.rs @@ -20,7 +20,6 @@ use ide_db::{ }; use itertools::Itertools; use profile::{memory_usage, Bytes}; -use std::env; use stdx::format_to; use syntax::{ast, Parse, SyntaxNode}; use triomphe::Arc; diff --git a/crates/profile/src/stop_watch.rs b/crates/profile/src/stop_watch.rs index 990b59cad4..0a803959ee 100644 --- a/crates/profile/src/stop_watch.rs +++ b/crates/profile/src/stop_watch.rs @@ -29,11 +29,10 @@ impl StopWatch { // When debugging rust-analyzer using rr, the perf-related syscalls cause it to abort. // We allow disabling perf by setting the env var `RA_DISABLE_PERF`. - use once_cell::sync::Lazy; - static PERF_ENABLED: Lazy = - Lazy::new(|| std::env::var_os("RA_DISABLE_PERF").is_none()); + use std::sync::OnceLock; + static PERF_ENABLED: OnceLock = OnceLock::new(); - if *PERF_ENABLED { + if *PERF_ENABLED.get_or_init(|| std::env::var_os("RA_DISABLE_PERF").is_none()) { let mut counter = perf_event::Builder::new() .build() .map_err(|err| eprintln!("Failed to create perf counter: {err}")) diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs index 744f524ff8..26cdf40a6c 100644 --- a/xtask/src/codegen.rs +++ b/xtask/src/codegen.rs @@ -5,7 +5,10 @@ use std::{ use xshell::{cmd, Shell}; -use crate::{flags, project_root}; +use crate::{ + flags::{self, CodegenType}, + project_root, +}; pub(crate) mod assists_doc_tests; pub(crate) mod diagnostics_docs; @@ -175,9 +178,8 @@ fn reformat(text: String) -> String { stdout } -fn add_preamble(generator: &'static str, mut text: String) -> String { - let preamble = - format!("//! Generated by `cargo codegen {generator}`, do not edit by hand.\n\n"); +fn add_preamble(cg: CodegenType, mut text: String) -> String { + let preamble = format!("//! Generated by `cargo codegen {cg}`, do not edit by hand.\n\n"); text.insert_str(0, &preamble); text } diff --git a/xtask/src/codegen/assists_doc_tests.rs b/xtask/src/codegen/assists_doc_tests.rs index 4edf1fdaf3..eab288c11b 100644 --- a/xtask/src/codegen/assists_doc_tests.rs +++ b/xtask/src/codegen/assists_doc_tests.rs @@ -45,7 +45,7 @@ r#####" buf.push_str(&test) } } - let buf = add_preamble("assists-doc-tests", reformat(buf)); + let buf = add_preamble(crate::flags::CodegenType::AssistsDocTests, reformat(buf)); ensure_file_contents( &project_root().join("crates/ide-assists/src/tests/generated.rs"), &buf, @@ -59,7 +59,7 @@ r#####" // a release. let contents = add_preamble( - "sourcegen_assists_docs", + crate::flags::CodegenType::AssistsDocTests, assists.into_iter().map(|it| it.to_string()).collect::>().join("\n\n"), ); let dst = project_root().join("docs/user/generated_assists.adoc"); diff --git a/xtask/src/codegen/diagnostics_docs.rs b/xtask/src/codegen/diagnostics_docs.rs index ba26ffc4da..316ae80f4c 100644 --- a/xtask/src/codegen/diagnostics_docs.rs +++ b/xtask/src/codegen/diagnostics_docs.rs @@ -12,7 +12,7 @@ pub(crate) fn generate(check: bool) { if !check { let contents = diagnostics.into_iter().map(|it| it.to_string()).collect::>().join("\n\n"); - let contents = add_preamble("diagnostics-docs", contents); + let contents = add_preamble(crate::flags::CodegenType::DiagnosticsDocs, contents); let dst = project_root().join("docs/user/generated_diagnostic.adoc"); fs::write(dst, contents).unwrap(); } diff --git a/xtask/src/codegen/grammar.rs b/xtask/src/codegen/grammar.rs index e3d2bfcfef..05c7132c0d 100644 --- a/xtask/src/codegen/grammar.rs +++ b/xtask/src/codegen/grammar.rs @@ -69,7 +69,7 @@ fn generate_tokens(grammar: &AstSrc) -> String { }); add_preamble( - "grammar", + crate::flags::CodegenType::Grammar, reformat( quote! { use crate::{SyntaxKind::{self, *}, SyntaxToken, ast::AstToken}; @@ -328,7 +328,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> String { } } - let res = add_preamble("grammar", reformat(res)); + let res = add_preamble(crate::flags::CodegenType::Grammar, reformat(res)); res.replace("#[derive", "\n#[derive") } @@ -458,7 +458,7 @@ fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> String { } }; - add_preamble("grammar", reformat(ast.to_string())) + add_preamble(crate::flags::CodegenType::Grammar, reformat(ast.to_string())) } fn to_upper_snake_case(s: &str) -> String { diff --git a/xtask/src/codegen/lints.rs b/xtask/src/codegen/lints.rs index d4cc0d01a7..5b933933c3 100644 --- a/xtask/src/codegen/lints.rs +++ b/xtask/src/codegen/lints.rs @@ -73,7 +73,7 @@ pub struct LintGroup { .unwrap(); generate_descriptor_clippy(&mut contents, &lints_json); - let contents = add_preamble("lint-definitions", reformat(contents)); + let contents = add_preamble(crate::flags::CodegenType::LintDefinitions, reformat(contents)); let destination = project_root().join(DESTINATION); ensure_file_contents(destination.as_path(), &contents, check); diff --git a/xtask/src/flags.rs b/xtask/src/flags.rs index 00fa3117b3..4bc844ab7e 100644 --- a/xtask/src/flags.rs +++ b/xtask/src/flags.rs @@ -1,6 +1,6 @@ #![allow(unreachable_pub)] -use std::str::FromStr; +use std::{fmt, str::FromStr}; use crate::install::{ClientOpt, ServerOpt}; @@ -187,6 +187,18 @@ pub enum CodegenType { LintDefinitions, } +impl fmt::Display for CodegenType { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::All => write!(f, "all"), + Self::Grammar => write!(f, "grammar"), + Self::AssistsDocTests => write!(f, "assists-doc-tests"), + Self::DiagnosticsDocs => write!(f, "diagnostics-docs"), + Self::LintDefinitions => write!(f, "lint-definitions"), + } + } +} + impl FromStr for CodegenType { type Err = String; fn from_str(s: &str) -> Result {