use cpp_parser::{CppTokenData, CppTokenKind, SourceRange};
use crate::{
macros::{MacroDef, parse_define},
token::{Token, is_trivia},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DirectiveKind {
If,
Ifdef,
Ifndef,
Elif,
Else,
Endif,
Define,
Undef,
Include,
IncludeNext,
Pragma,
Error,
Warning,
Line,
Null,
Unknown,
}
impl DirectiveKind {
pub fn from_name(name: &str) -> DirectiveKind {
match name {
"if" => DirectiveKind::If,
"ifdef" => DirectiveKind::Ifdef,
"ifndef" => DirectiveKind::Ifndef,
"elif" => DirectiveKind::Elif,
"else" => DirectiveKind::Else,
"endif" => DirectiveKind::Endif,
"define" => DirectiveKind::Define,
"undef" => DirectiveKind::Undef,
"include" => DirectiveKind::Include,
"include_next" => DirectiveKind::IncludeNext,
"pragma" => DirectiveKind::Pragma,
"error" => DirectiveKind::Error,
"warning" => DirectiveKind::Warning,
"line" => DirectiveKind::Line,
_ => DirectiveKind::Unknown,
}
}
pub fn name(self) -> &'static str {
match self {
DirectiveKind::If => "if",
DirectiveKind::Ifdef => "ifdef",
DirectiveKind::Ifndef => "ifndef",
DirectiveKind::Elif => "elif",
DirectiveKind::Else => "else",
DirectiveKind::Endif => "endif",
DirectiveKind::Define => "define",
DirectiveKind::Undef => "undef",
DirectiveKind::Include => "include",
DirectiveKind::IncludeNext => "include_next",
DirectiveKind::Pragma => "pragma",
DirectiveKind::Error => "error",
DirectiveKind::Warning => "warning",
DirectiveKind::Line => "line",
DirectiveKind::Null => "#",
DirectiveKind::Unknown => "",
}
}
pub fn opens_a_condition(self) -> bool {
matches!(
self,
DirectiveKind::If | DirectiveKind::Ifdef | DirectiveKind::Ifndef
)
}
pub fn closes_a_condition(self) -> bool {
matches!(
self,
DirectiveKind::Elif | DirectiveKind::Else | DirectiveKind::Endif
)
}
pub fn changes_macros(self) -> bool {
matches!(self, DirectiveKind::Define | DirectiveKind::Undef)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum IncludeForm {
Angle,
Quote,
Macro,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Include {
pub form: IncludeForm,
pub target: Box<str>,
pub is_next: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Define {
pub macro_def: Option<MacroDef>,
pub is_redefinition: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Directive {
Conditional {
kind: DirectiveKind,
condition: Vec<Token>,
},
Ifdef {
kind: DirectiveKind,
name: Box<str>,
},
Marker {
kind: DirectiveKind,
},
Define(Define),
Undef {
name: Option<Box<str>>,
},
Include(Include),
Pragma {
tokens: Vec<Token>,
},
Diagnostic {
kind: DirectiveKind,
message: String,
},
Line {
tokens: Vec<Token>,
},
Null,
Other {
name: Box<str>,
},
}
impl Directive {
pub fn kind(&self) -> DirectiveKind {
match self {
Directive::Conditional { kind, .. }
| Directive::Ifdef { kind, .. }
| Directive::Marker { kind }
| Directive::Diagnostic { kind, .. } => *kind,
Directive::Define(_) => DirectiveKind::Define,
Directive::Undef { .. } => DirectiveKind::Undef,
Directive::Include(include) => {
if include.is_next {
DirectiveKind::IncludeNext
} else {
DirectiveKind::Include
}
}
Directive::Pragma { .. } => DirectiveKind::Pragma,
Directive::Line { .. } => DirectiveKind::Line,
Directive::Null => DirectiveKind::Null,
Directive::Other { .. } => DirectiveKind::Unknown,
}
}
pub fn defines(&self) -> Option<&MacroDef> {
match self {
Directive::Define(define) => define.macro_def.as_ref(),
_ => None,
}
}
pub fn undefines(&self) -> Option<&str> {
match self {
Directive::Undef { name } => name.as_deref(),
_ => None,
}
}
pub fn as_include(&self) -> Option<&Include> {
match self {
Directive::Include(include) => Some(include),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SpannedDirective {
pub directive: Directive,
pub range: SourceRange,
pub line: SourceRange,
pub condition_depth: usize,
}
pub fn scan_directives(source: &str, tokens: &[CppTokenData]) -> Vec<SpannedDirective> {
let mut out = Vec::new();
let mut depth = 0usize;
let mut index = 0usize;
while index < tokens.len() {
if tokens[index].kind != CppTokenKind::Hash || !starts_its_logical_line(tokens, index) {
index += 1;
continue;
}
let mut end = index;
while end < tokens.len() && tokens[end].kind != CppTokenKind::Newline {
end += 1;
}
let mut after = if end < tokens.len() { end + 1 } else { end };
while after < tokens.len() && is_trivia(tokens[after].kind) {
after += 1;
}
let start_offset = tokens[index].range.start_offset;
let end_offset = tokens
.get(after)
.map_or(source.len(), |token| token.range.start_offset);
let range = SourceRange::new(start_offset, end_offset.saturating_sub(start_offset));
let mut own = end;
while own > index && is_trivia(tokens[own - 1].kind) {
own -= 1;
}
let own_end = tokens
.get(own.saturating_sub(1))
.map_or(start_offset, |token| token.range.end_offset());
let line = SourceRange::new(start_offset, own_end.saturating_sub(start_offset));
let directive = parse_directive_tokens(&to_tokens(source, &tokens[index..after]), range);
let kind = directive.kind();
if kind == DirectiveKind::Endif {
out.push(SpannedDirective {
directive,
range,
line,
condition_depth: depth.saturating_sub(1),
});
depth = depth.saturating_sub(1);
index = after.max(index + 1);
continue;
}
out.push(SpannedDirective {
directive,
range,
line,
condition_depth: depth,
});
if kind.opens_a_condition() {
depth += 1;
}
index = after.max(index + 1);
}
out
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DirectiveSignature {
pub environment: u64,
pub layout: u64,
pub preamble_end: usize,
}
pub fn directive_signature(source: &str) -> DirectiveSignature {
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
let mut environment = FNV_OFFSET;
let mut layout = FNV_OFFSET;
let mut preamble_end = 0;
for directive in scan_directives(source, &tokens) {
let line = directive.line;
let text = source
.get(line.start_offset..line.end_offset())
.unwrap_or_default();
preamble_end = preamble_end.max(line.end_offset());
environment = fnv_mix(environment, text.as_bytes());
environment = fnv_mix(environment, &[0xff]);
layout = fnv_mix(layout, &(line.start_offset as u64).to_le_bytes());
layout = fnv_mix(layout, text.as_bytes());
layout = fnv_mix(layout, &[0xff]);
}
DirectiveSignature {
environment,
layout,
preamble_end,
}
}
pub fn preamble_end(source: &str) -> usize {
directive_signature(source).preamble_end
}
const FNV_OFFSET: u64 = 0xcbf2_9ce4_8422_2325;
fn fnv_mix(mut hash: u64, bytes: &[u8]) -> u64 {
for byte in bytes {
hash ^= u64::from(*byte);
hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
}
hash
}
fn starts_its_logical_line(tokens: &[CppTokenData], index: usize) -> bool {
let mut i = index;
while i > 0 {
i -= 1;
if tokens[i].kind == CppTokenKind::Newline {
return true;
}
if !is_trivia(tokens[i].kind) {
return false;
}
}
true
}
pub(super) fn to_tokens(source: &str, tokens: &[CppTokenData]) -> Vec<Token> {
tokens
.iter()
.map(|token| {
let text = &source[token.range.start_offset..token.range.end_offset()];
Token::new(token.kind, text, token.range)
})
.collect()
}
pub fn parse_directive_tokens(tokens: &[Token], range: SourceRange) -> Directive {
let mut index = 0;
skip_trivia(tokens, &mut index);
match tokens.get(index).map(|token| token.kind) {
Some(CppTokenKind::Hash) => index += 1,
_ => return Directive::Null,
}
skip_trivia(tokens, &mut index);
let Some(name) = tokens.get(index) else {
return Directive::Null;
};
let kind = DirectiveKind::from_name(name.text());
index += 1;
let rest = strip_leading_trivia(&tokens[index.min(tokens.len())..]);
match kind {
DirectiveKind::If | DirectiveKind::Elif => Directive::Conditional {
kind,
condition: significant(rest),
},
DirectiveKind::Ifdef | DirectiveKind::Ifndef => match first_identifier(rest) {
Some(name) => Directive::Ifdef { kind, name },
None => Directive::Other {
name: kind.name().into(),
},
},
DirectiveKind::Else | DirectiveKind::Endif => Directive::Marker { kind },
DirectiveKind::Define => match parse_define(rest, range) {
Some(macro_def) => Directive::Define(Define {
macro_def: Some(macro_def),
is_redefinition: false,
}),
None => Directive::Define(Define {
macro_def: None,
is_redefinition: false,
}),
},
DirectiveKind::Undef => Directive::Undef {
name: first_identifier(rest),
},
DirectiveKind::Include | DirectiveKind::IncludeNext => parse_include(rest, kind),
DirectiveKind::Pragma => Directive::Pragma {
tokens: significant(rest),
},
DirectiveKind::Error | DirectiveKind::Warning => Directive::Diagnostic {
kind,
message: message_text(rest),
},
DirectiveKind::Line => Directive::Line {
tokens: significant(rest),
},
DirectiveKind::Null | DirectiveKind::Unknown => Directive::Other {
name: name.text.as_str().into(),
},
}
}
fn parse_include(tokens: &[Token], kind: DirectiveKind) -> Directive {
let is_next = kind == DirectiveKind::IncludeNext;
let Some(first) = tokens.iter().find(|token| !is_trivia(token.kind)) else {
return Directive::Other {
name: kind.name().into(),
};
};
let (form, target) = match first.kind {
CppTokenKind::HeaderName => {
let text = first.text();
if text.starts_with('<') {
(
IncludeForm::Angle,
text.trim_start_matches('<').trim_end_matches('>'),
)
} else {
(
IncludeForm::Quote,
text.trim_start_matches('"').trim_end_matches('"'),
)
}
}
CppTokenKind::Less => {
let mut text = String::new();
for token in tokens
.iter()
.skip_while(|token| token.kind != CppTokenKind::Less)
.skip(1)
{
if token.kind == CppTokenKind::Greater {
return Directive::Include(Include {
form: IncludeForm::Angle,
target: text.into(),
is_next,
});
}
text.push_str(token.text());
}
return Directive::Other {
name: kind.name().into(),
};
}
CppTokenKind::Identifier => {
return Directive::Include(Include {
form: IncludeForm::Macro,
target: first.text.as_str().into(),
is_next,
});
}
_ => {
return Directive::Other {
name: kind.name().into(),
};
}
};
Directive::Include(Include {
form,
target: target.into(),
is_next,
})
}
fn message_text(tokens: &[Token]) -> String {
let mut out = String::new();
for token in tokens.iter().filter(|token| !is_trivia(token.kind)) {
if !out.is_empty() {
out.push(' ');
}
out.push_str(token.text());
}
out
}
fn first_identifier(tokens: &[Token]) -> Option<Box<str>> {
tokens
.iter()
.find(|token| !is_trivia(token.kind))
.filter(|token| token.is_identifier())
.map(|token| token.text.as_str().into())
}
fn significant(tokens: &[Token]) -> Vec<Token> {
tokens
.iter()
.filter(|token| !is_trivia(token.kind))
.cloned()
.collect()
}
fn strip_leading_trivia(tokens: &[Token]) -> &[Token] {
let mut index = 0;
skip_trivia(tokens, &mut index);
&tokens[index..]
}
fn skip_trivia(tokens: &[Token], index: &mut usize) {
while tokens
.get(*index)
.is_some_and(|token| is_trivia(token.kind))
{
*index += 1;
}
}
pub(crate) fn fold_quoted_header_names(source: &str, tokens: &mut [CppTokenData]) {
let next_significant = |tokens: &[CppTokenData], from: usize| {
(from..tokens.len()).find(|&at| !cpp_parser::is_trivia(tokens[at].kind))
};
for at in 0..tokens.len() {
if tokens[at].kind != CppTokenKind::Hash {
continue;
}
let Some(name) = next_significant(tokens, at + 1) else {
continue;
};
let word = &source[tokens[name].range.start_offset..tokens[name].range.end_offset()];
if word != "include" && word != "include_next" {
continue;
}
let Some(target) = next_significant(tokens, name + 1) else {
continue;
};
if tokens[target].kind != CppTokenKind::StringLiteral {
continue;
}
let text = &source[tokens[target].range.start_offset..tokens[target].range.end_offset()];
if !text.contains('\\') {
tokens[target].kind = CppTokenKind::HeaderName;
}
}
}