diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:03 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:03 +0000 |
commit | 64d98f8ee037282c35007b64c2649055c56af1db (patch) | |
tree | 5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /vendor/clap_derive/src/utils | |
parent | Adding debian version 1.67.1+dfsg1-1. (diff) | |
download | rustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip |
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/clap_derive/src/utils')
-rw-r--r-- | vendor/clap_derive/src/utils/doc_comments.rs | 87 | ||||
-rw-r--r-- | vendor/clap_derive/src/utils/mod.rs | 4 | ||||
-rw-r--r-- | vendor/clap_derive/src/utils/spanned.rs | 9 | ||||
-rw-r--r-- | vendor/clap_derive/src/utils/ty.rs | 58 |
4 files changed, 110 insertions, 48 deletions
diff --git a/vendor/clap_derive/src/utils/doc_comments.rs b/vendor/clap_derive/src/utils/doc_comments.rs index f0a5034d7..5183b6b25 100644 --- a/vendor/clap_derive/src/utils/doc_comments.rs +++ b/vendor/clap_derive/src/utils/doc_comments.rs @@ -3,43 +3,58 @@ //! #[derive(Parser)] works in terms of "paragraphs". Paragraph is a sequence of //! non-empty adjacent lines, delimited by sequences of blank (whitespace only) lines. -use crate::attrs::Method; - -use quote::{format_ident, quote}; use std::iter; -pub fn process_doc_comment(lines: Vec<String>, name: &str, preprocess: bool) -> Vec<Method> { +pub fn extract_doc_comment(attrs: &[syn::Attribute]) -> Vec<String> { + use syn::Lit::*; + use syn::Meta::*; + use syn::MetaNameValue; + // multiline comments (`/** ... */`) may have LFs (`\n`) in them, // we need to split so we could handle the lines correctly // // we also need to remove leading and trailing blank lines - let mut lines: Vec<&str> = lines + let mut lines: Vec<_> = attrs .iter() + .filter(|attr| attr.path.is_ident("doc")) + .filter_map(|attr| { + if let Ok(NameValue(MetaNameValue { lit: Str(s), .. })) = attr.parse_meta() { + Some(s.value()) + } else { + // non #[doc = "..."] attributes are not our concern + // we leave them for rustc to handle + None + } + }) .skip_while(|s| is_blank(s)) - .flat_map(|s| s.split('\n')) + .flat_map(|s| { + let lines = s + .split('\n') + .map(|s| { + // remove one leading space no matter what + let s = s.strip_prefix(' ').unwrap_or(s); + s.to_owned() + }) + .collect::<Vec<_>>(); + lines + }) .collect(); while let Some(true) = lines.last().map(|s| is_blank(s)) { lines.pop(); } - // remove one leading space no matter what - for line in lines.iter_mut() { - if line.starts_with(' ') { - *line = &line[1..]; - } - } - - if lines.is_empty() { - return vec![]; - } - - let short_name = format_ident!("{}", name); - let long_name = format_ident!("long_{}", name); + lines +} +pub fn format_doc_comment( + lines: &[String], + preprocess: bool, + force_long: bool, +) -> (Option<String>, Option<String>) { if let Some(first_blank) = lines.iter().position(|s| is_blank(s)) { let (short, long) = if preprocess { - let paragraphs = split_paragraphs(&lines); + let paragraphs = split_paragraphs(lines); let short = paragraphs[0].clone(); let long = paragraphs.join("\n\n"); (remove_period(short), long) @@ -49,26 +64,24 @@ pub fn process_doc_comment(lines: Vec<String>, name: &str, preprocess: bool) -> (short, long) }; - vec![ - Method::new(short_name, quote!(#short)), - Method::new(long_name, quote!(#long)), - ] + (Some(short), Some(long)) } else { - let short = if preprocess { - let s = merge_lines(&lines); - remove_period(s) + let (short, long) = if preprocess { + let short = merge_lines(lines); + let long = force_long.then(|| short.clone()); + let short = remove_period(short); + (short, long) } else { - lines.join("\n") + let short = lines.join("\n"); + let long = force_long.then(|| short.clone()); + (short, long) }; - vec![ - Method::new(short_name, quote!(#short)), - Method::new(long_name, quote!(None)), - ] + (Some(short), long) } } -fn split_paragraphs(lines: &[&str]) -> Vec<String> { +fn split_paragraphs(lines: &[String]) -> Vec<String> { let mut last_line = 0; iter::from_fn(|| { let slice = &lines[last_line..]; @@ -102,6 +115,10 @@ fn is_blank(s: &str) -> bool { s.trim().is_empty() } -fn merge_lines(lines: &[&str]) -> String { - lines.iter().map(|s| s.trim()).collect::<Vec<_>>().join(" ") +fn merge_lines(lines: impl IntoIterator<Item = impl AsRef<str>>) -> String { + lines + .into_iter() + .map(|s| s.as_ref().trim().to_owned()) + .collect::<Vec<_>>() + .join(" ") } diff --git a/vendor/clap_derive/src/utils/mod.rs b/vendor/clap_derive/src/utils/mod.rs index 77a467c75..9f8b6f380 100644 --- a/vendor/clap_derive/src/utils/mod.rs +++ b/vendor/clap_derive/src/utils/mod.rs @@ -2,8 +2,10 @@ mod doc_comments; mod spanned; mod ty; +pub use doc_comments::extract_doc_comment; +pub use doc_comments::format_doc_comment; + pub use self::{ - doc_comments::process_doc_comment, spanned::Sp, ty::{inner_type, is_simple_ty, sub_type, subty_if_name, Ty}, }; diff --git a/vendor/clap_derive/src/utils/spanned.rs b/vendor/clap_derive/src/utils/spanned.rs index 11415f6f0..339a654e6 100644 --- a/vendor/clap_derive/src/utils/spanned.rs +++ b/vendor/clap_derive/src/utils/spanned.rs @@ -5,7 +5,7 @@ use syn::LitStr; use std::ops::{Deref, DerefMut}; /// An entity with a span attached. -#[derive(Debug, Clone)] +#[derive(Debug, Copy, Clone)] pub struct Sp<T> { val: T, span: Span, @@ -16,11 +16,8 @@ impl<T> Sp<T> { Sp { val, span } } - pub fn call_site(val: T) -> Self { - Sp { - val, - span: Span::call_site(), - } + pub fn get(&self) -> &T { + &self.val } pub fn span(&self) -> Span { diff --git a/vendor/clap_derive/src/utils/ty.rs b/vendor/clap_derive/src/utils/ty.rs index 0bcb59f27..9349bc29d 100644 --- a/vendor/clap_derive/src/utils/ty.rs +++ b/vendor/clap_derive/src/utils/ty.rs @@ -7,12 +7,15 @@ use syn::{ PathSegment, Type, TypePath, }; -#[derive(Copy, Clone, PartialEq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug)] pub enum Ty { + Unit, Vec, + VecVec, Option, OptionOption, OptionVec, + OptionVecVec, Other, } @@ -21,13 +24,15 @@ impl Ty { use self::Ty::*; let t = |kind| Sp::new(kind, ty.span()); - if is_generic_ty(ty, "Vec") { - t(Vec) + if is_unit_ty(ty) { + t(Unit) + } else if let Some(vt) = get_vec_ty(ty, Vec, VecVec) { + t(vt) } else if let Some(subty) = subty_if_name(ty, "Option") { if is_generic_ty(subty, "Option") { t(OptionOption) - } else if is_generic_ty(subty, "Vec") { - t(OptionVec) + } else if let Some(vt) = get_vec_ty(subty, OptionVec, OptionVecVec) { + t(vt) } else { t(Option) } @@ -35,15 +40,32 @@ impl Ty { t(Other) } } + + pub fn as_str(&self) -> &'static str { + match self { + Self::Unit => "()", + Self::Vec => "Vec<T>", + Self::Option => "Option<T>", + Self::OptionOption => "Option<Option<T>>", + Self::OptionVec => "Option<Vec<T>>", + Self::VecVec => "Vec<Vec<T>>", + Self::OptionVecVec => "Option<Vec<Vec<T>>>", + Self::Other => "...other...", + } + } } pub fn inner_type(field_ty: &syn::Type) -> &syn::Type { let ty = Ty::from_syn_ty(field_ty); match *ty { Ty::Vec | Ty::Option => sub_type(field_ty).unwrap_or(field_ty), - Ty::OptionOption | Ty::OptionVec => { + Ty::OptionOption | Ty::OptionVec | Ty::VecVec => { sub_type(field_ty).and_then(sub_type).unwrap_or(field_ty) } + Ty::OptionVecVec => sub_type(field_ty) + .and_then(sub_type) + .and_then(sub_type) + .unwrap_or(field_ty), _ => field_ty, } } @@ -111,9 +133,33 @@ fn is_generic_ty(ty: &syn::Type, name: &str) -> bool { subty_if_name(ty, name).is_some() } +fn is_unit_ty(ty: &syn::Type) -> bool { + if let syn::Type::Tuple(tuple) = ty { + tuple.elems.is_empty() + } else { + false + } +} + fn only_one<I, T>(mut iter: I) -> Option<T> where I: Iterator<Item = T>, { iter.next().filter(|_| iter.next().is_none()) } + +#[cfg(feature = "unstable-v5")] +fn get_vec_ty(ty: &Type, vec_ty: Ty, vecvec_ty: Ty) -> Option<Ty> { + subty_if_name(ty, "Vec").map(|subty| { + if is_generic_ty(subty, "Vec") { + vecvec_ty + } else { + vec_ty + } + }) +} + +#[cfg(not(feature = "unstable-v5"))] +fn get_vec_ty(ty: &Type, vec_ty: Ty, _vecvec_ty: Ty) -> Option<Ty> { + is_generic_ty(ty, "Vec").then_some(vec_ty) +} |