Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/codegraph-bench/fixtures/cpp/namespaced.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "base.hpp"

namespace ns {
void compute() {}
}

void run_namespaced() {
ns::compute();
}
6 changes: 6 additions & 0 deletions crates/codegraph-bench/fixtures/cpp/templated_call.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
template <typename T>
void process(T value) {}

void run_templated() {
process<int>(0);
}
11 changes: 11 additions & 0 deletions crates/codegraph-bench/fixtures/cpp/ue_actor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

class ENGINE_API UFoo : public UObject
{
GENERATED_BODY()
UPROPERTY(EditAnywhere)
int Health;
ENGINE_API virtual void Tick();
UFUNCTION()
void Bar();
};
90 changes: 89 additions & 1 deletion crates/codegraph-extract/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ use codegraph_core::config::IndexingConfig;
use codegraph_core::node_id::hash_content;
use codegraph_core::types::{ExtractionResult, Language};
use rayon::prelude::*;
use regex::Regex;
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use std::time::Instant;
use tree_sitter::Parser;

Expand Down Expand Up @@ -110,13 +112,58 @@ pub fn detect_language(file_path: impl AsRef<Path>) -> Language {
Language::Unknown
}

/// A `.h` file maps to `Language::C` by extension, but may hold C++ or
/// Objective-C. Match the upstream 8 KB-prefix content sniff (`grammars.ts`
/// `looksLikeCpp`). The `class MACRO Name : Base` alternative recognizes an
/// export-macro-annotated class whose only C++ signal is the macro — the
/// two-token `<KW> <MACRO> <Name>` shape never occurs in valid C, so genuine C
/// headers stay C. Lookahead-free (the `regex` crate has none).
fn looks_like_cpp(source: &str) -> bool {
static RE: OnceLock<Regex> = OnceLock::new();
let re = RE.get_or_init(|| {
Regex::new(
r"\bnamespace\b|\bclass\s+\w+\s*[:{]|\b(?:class|struct)\s+[A-Z][A-Z0-9_]+\s+\w+\s*(?:final\s*)?[:{]|\btemplate\s*<|\b(?:public|private|protected)\s*:|\bvirtual\b|\busing\s+(?:namespace\b|\w+\s*=)",
)
.expect("looks-like-cpp regex")
});
re.is_match(prefix_8k(source))
}

fn looks_like_objc(source: &str) -> bool {
static RE: OnceLock<Regex> = OnceLock::new();
let re = RE.get_or_init(|| {
Regex::new(r"@(?:interface|implementation|protocol|synthesize)\b")
.expect("looks-like-objc regex")
});
re.is_match(prefix_8k(source))
}

fn prefix_8k(source: &str) -> &str {
match source.char_indices().nth(8192) {
Some((idx, _)) => &source[..idx],
None => source,
}
}

pub fn extract_source(
file_path: &str,
source: &str,
language: Option<Language>,
) -> ExtractionResult {
let start = Instant::now();
let language = language.unwrap_or_else(|| detect_language(file_path));
let mut language = language.unwrap_or_else(|| detect_language(file_path));
if language == Language::C
&& Path::new(file_path)
.extension()
.and_then(|ext| ext.to_str())
.is_some_and(|ext| ext.eq_ignore_ascii_case("h"))
{
if looks_like_cpp(source) {
language = Language::Cpp;
} else if looks_like_objc(source) {
language = Language::ObjC;
}
}
if let Some(result) = crate::embedded::extract_embedded(file_path, source, language) {
return result;
}
Expand Down Expand Up @@ -974,4 +1021,45 @@ mod tests {
merged.nodes.iter().map(|n| &n.name).collect::<Vec<_>>()
);
}

#[test]
fn looks_like_cpp_export_macro_class() {
assert!(looks_like_cpp(
"class ENGINE_API UFoo : public UObject { };"
));
}

#[test]
fn looks_like_cpp_plain_c_false() {
assert!(!looks_like_cpp("struct Foo { int x; };\nvoid f(void);\n"));
}

#[test]
fn looks_like_objc_interface() {
assert!(looks_like_objc("@interface Foo\n@end\n"));
}

#[test]
fn dot_h_reclassified_to_cpp_by_content() {
let result = extract_source(
"F.h",
"class ENGINE_API UFoo : public UObject { GENERATED_BODY() };",
None,
);
assert!(
result.nodes.iter().any(|n| n.name == "UFoo"),
"a UE .h should be reclassified to C++ and yield a UFoo class node, got: {:?}",
result
.nodes
.iter()
.map(|n| (n.kind, n.name.as_str()))
.collect::<Vec<_>>()
);
}

#[test]
fn dot_h_plain_c_stays_c() {
let result = extract_source("plain.h", "int add(int a, int b);\n", None);
assert!(result.errors.is_empty(), "errors: {:?}", result.errors);
}
}
Loading
Loading