Fix stop_watch on linux

This commit is contained in:
Lukas Wirth 2024-07-07 08:39:08 +02:00
parent 9b3e912d67
commit 15973f1a55
8 changed files with 29 additions and 17 deletions

View file

@ -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;

View file

@ -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<bool> =
Lazy::new(|| std::env::var_os("RA_DISABLE_PERF").is_none());
use std::sync::OnceLock;
static PERF_ENABLED: OnceLock<bool> = 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}"))

View file

@ -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
}

View file

@ -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::<Vec<_>>().join("\n\n"),
);
let dst = project_root().join("docs/user/generated_assists.adoc");

View file

@ -12,7 +12,7 @@ pub(crate) fn generate(check: bool) {
if !check {
let contents =
diagnostics.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().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();
}

View file

@ -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 {

View file

@ -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);

View file

@ -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<Self, Self::Err> {