Skip to content

Export a C or C++ header document as .h or .hpp [minor] - #368

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/367-header-export-extension
Nov 4, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/367-header-export-extension

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #367

What was wrong

A SourceFile with IsHeader is generated with #pragma once, but CoderEditorApp.Export always named the output {stem}.{generator.FileExtension}. A header document therefore came out as Physics.cpp or Physics.c, which an #include "Physics.hpp" can't find and a *.cpp glob compiles as its own translation unit.

Change

  • ILanguageGenerator.FileExtensionFor(AstNode) is a new default interface member that returns FileExtension. LanguageGeneratorBase implements it as a virtual method. (It isn't called GetFileExtension because CA1721 rejects that name next to the FileExtension property.)
  • CFamilyGenerator overrides it and returns HeaderFileExtension for a SourceFile { IsHeader: true }. HeaderFileExtension is virtual and defaults to h. C++ overrides it to hpp, so a C++ header isn't mistaken for one a C compiler can read.
  • CoderEditorApp.Export asks the generator for the extension of the document's root.
  • Documents that aren't headers keep .c and .cpp. Every other language ignores the flag.

This is marked [minor] because it adds public API (the interface member is defaulted and the new property is virtual, so existing implementers still build).

Tests

  • CoderEditorAppTests.Export_NamesAHeaderDocumentAsAHeader covers C and C++, header and non-header. It checks the exported path and whether the file carries #pragma once.
  • GeneratorFileExtensionTests covers the C-family header extensions, and checks that every generator keeps its ordinary extension for a non-header and that the non-C-family languages ignore IsHeader.
  • With the behaviour reverted (the API kept so the tests compile), 3 tests fail. With the fix, the full suite passes: 1558 of 1558.

Generated by Claude Code

A SourceFile marked IsHeader is generated with #pragma once, but the
editor's export always named the file with the generator's fixed
extension, so a header came out as Physics.cpp / Physics.c.

ILanguageGenerator gains FileExtensionFor(AstNode), defaulting to
FileExtension. CFamilyGenerator answers HeaderFileExtension for a header
SourceFile: "h" for C, "hpp" for C++. The editor's Export asks for the
extension of the document it is writing.

Fixes #367

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW1rKnQJLnGSj1mbcDM
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exporting a C/C++ header document writes Name.cpp / Name.c containing #pragma once instead of a .h/.hpp header

1 participant