Skip to content
Closed
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
24 changes: 24 additions & 0 deletions __tests__/extraction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,30 @@ class ENGINE_API UNetConnectionRepControl : public UObject
expect(result.nodes.find((n) => n.name === 'Base')?.kind).toBe('struct');
});

it('should detect declaration-only C++ .h headers with templates, references, or default arguments as cpp', () => {
const pdbDiaHeader = `#pragma once
extern CComPtr<IDiaDataSource> g_pDiaDataSource;
extern bool g_dia_available;
qstring get_dia_type_declaration(IDiaSymbol* type, int depth = 0);
`;
expect(detectLanguage('PdbDiaSession.h', pdbDiaHeader)).toBe('cpp');
const res1 = extractFromSource('PdbDiaSession.h', pdbDiaHeader);
expect(res1.errors).toEqual([]);
expect(res1.nodes.some((n) => n.name === 'g_pDiaDataSource' && n.kind === 'variable')).toBe(true);

const programHeader = `#pragma once
HRESULT GetMaxVersionVsInstallationPath(bstr_t& bstrVsInstallationPath, ULONGLONG& ullVersion, qvector<qstring>* installation_paths = nullptr);
`;
expect(detectLanguage('Program.h', programHeader)).toBe('cpp');
expect(extractFromSource('Program.h', programHeader).errors).toEqual([]);

const oldPdbHeader = `bool apply_name(ea_t ea, const qstring &name, int maybe_func);
void load_vc_til(void);
`;
expect(detectLanguage('oldpdb.h', oldPdbHeader)).toBe('cpp');
expect(extractFromSource('oldpdb.h', oldPdbHeader).errors).toEqual([]);
});

it('should return unknown for unsupported extensions', () => {
expect(detectLanguage('styles.css')).toBe('unknown');
expect(detectLanguage('data.json')).toBe('unknown');
Expand Down
37 changes: 37 additions & 0 deletions src/extraction/grammars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ export function detectLanguage(filePath: string, source?: string, overrides?: Re
if (lang === 'c' && ext === '.h' && source) {
if (looksLikeCpp(source)) return 'cpp';
if (looksLikeObjc(source)) return 'objc';
if (looksLikeCppDeclarations(source)) return 'cpp';
}

// `.inc` is PHP's include extension (Drupal) and Pascal/Delphi's too
Expand Down Expand Up @@ -778,6 +779,42 @@ function looksLikeCpp(source: string): boolean {
return CPP_BASE_CLAUSE_RE.test(source.replace(C_COMMENT_RE, ' '));
}

const C_COMMENT_OR_STRING_RE = /\/\*[\s\S]*?\*\/|\/\/[^\n]*|"(?:[^"\\\n]|\\.)*"/g;

// Detect declaration-only C++ .h headers (without class/namespace/template definitions)
// via C++-only keywords, :: scope resolution, template type declarations, reference params, and default args.
function looksLikeCppDeclarations(source: string): boolean {
const code = source.replace(C_COMMENT_OR_STRING_RE, ' ');
if (
/\b(?:nullptr|constexpr|consteval|constinit|noexcept|decltype|typename|friend|explicit|mutable|co_await|co_yield|co_return|dynamic_cast|static_cast|reinterpret_cast|const_cast)\b/.test(
code
)
) {
return true;
}
if (/\b[A-Za-z_]\w*\s*::\s*[~A-Za-z_]/.test(code)) {
return true;
}
if (
/(?:^|[;{}(),]|\b(?:extern|static|const|volatile|inline|typedef)\s+)[ \t]*[A-Za-z_][\w:]*\s*<\s*[A-Za-z_][\w\s,:*&<>]*>\s*(?:[*&]+\s*)*[A-Za-z_]\w*\s*[;=,)\[(]/m.test(
code
)
) {
return true;
}
if (/\bconst\s+(?:(?:struct|class)\s+)?[A-Za-z_][\w:]*\s*(?:<\s*[^>;{}()]*>\s*)?[*&]*&\s*[A-Za-z_]\w*\s*[,)=;]/.test(code)) {
return true;
}
const fnDeclRe = /\b(?!(?:if|for|while|switch|return|sizeof|alignof|typeof|_Alignas|_Static_assert)\b)[A-Za-z_][\w:]*[\s*&]+[A-Za-z_]\w*\s*\(([^;{}()]+)\)\s*(?:const\b|noexcept\b|[;{])/g;
const refOrDefaultParamRe =
/(?:^|,)\s*(?:(?:const|volatile|unsigned|signed|short|long|struct|class|enum)\s+)*[A-Za-z_][\w:]*\s*(?:<\s*[^>()]*>\s*)?(?:[*&]*&\s*[A-Za-z_]\w*\s*(?:=[^,]+)?|[\s*]+[A-Za-z_]\w*\s*=\s*[^,]+)\s*(?:,|$)/;
let match: RegExpExecArray | null;
while ((match = fnDeclRe.exec(code)) !== null) {
if (refOrDefaultParamRe.test(match[1]!)) return true;
}
return false;
}

/**
* Heuristic: does a .h file contain Objective-C constructs?
*/
Expand Down