Skip to content

Commit c0eaa54

Browse files
jcfrhjmjohnson
andcommitted
feat: Extend Apple Framework header support to #include
Follow-up to the earlier patch that added framework handling for `__has_include`. This change updates `preprocess()` so that plain `#include <Pkg/MyHdr.h>` also resolves to `<Pkg.framework/Headers/MyHdr.h>` when present. Tests: - Add `appleFrameworkIncludeTest` to verify `#include` resolution. - Add dummy `Foundation.h` fixture under `testsuite/Foundation.framework/Headers/`. Co-authored-by: Hans Johnson <[email protected]>
1 parent 61eda0e commit c0eaa54

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

simplecpp.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3502,7 +3502,28 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
35023502

35033503
const bool systemheader = (inctok->str()[0] == '<');
35043504
const std::string header(inctok->str().substr(1U, inctok->str().size() - 2U));
3505-
const FileData *const filedata = cache.get(rawtok->location.file(), header, dui, systemheader, files, outputList).first;
3505+
3506+
// First, try normal resolution through the cache
3507+
const FileData * filedata = cache.get(rawtok->location.file(), header, dui, systemheader, files, outputList).first;
3508+
3509+
// Fallback: normal lookup failed. Use openHeader(...), which also handles Apple
3510+
// frameworks (e.g., "<Foo/Bar.h>" -> "<Foo.framework/Headers/Bar.h>").
3511+
// If openHeader(...) returns a resolved absolute path, load it via cache.get(...)
3512+
// with systemheader=false to treat it as a direct file (no system-header semantics)
3513+
// and outputList=nullptr to avoid emitting a duplicate "missing header" diagnostic
3514+
if (filedata == nullptr) {
3515+
std::ifstream f;
3516+
const std::string resolved =
3517+
openHeader(f, dui, rawtok->location.file(), header, systemheader);
3518+
if (!resolved.empty() && resolved != header) {
3519+
filedata = cache.get(rawtok->location.file(),
3520+
resolved,
3521+
dui,
3522+
/*systemheader=*/ false,
3523+
files,
3524+
/*outputList*/ nullptr).first;
3525+
}
3526+
}
35063527
if (filedata == nullptr) {
35073528
if (outputList) {
35083529
simplecpp::Output out(files);

test.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,6 +2131,32 @@ static void circularInclude()
21312131
ASSERT_EQUALS("", toString(outputList));
21322132
}
21332133

2134+
static void appleFrameworkIncludeTest()
2135+
{
2136+
// This test checks Apple framework include handling.
2137+
//
2138+
// If -I /tmp/testFrameworks
2139+
// and we write:
2140+
// #include <Foundation/Foundation.h>
2141+
//
2142+
// then simplecpp should find:
2143+
// ./testsuite/Foundation.framework/Headers/Foundation.h
2144+
const char code[] = "#include <Foundation/Foundation.h>\n";
2145+
std::vector<std::string> files;
2146+
const simplecpp::TokenList rawtokens = makeTokenList(code, files, "sourcecode.cpp");
2147+
simplecpp::FileDataCache cache;
2148+
simplecpp::TokenList tokens2(files);
2149+
simplecpp::DUI dui;
2150+
#ifdef SIMPLECPP_SOURCE_DIR
2151+
dui.includePaths.push_back(std::string(SIMPLECPP_SOURCE_DIR) + "/testsuite");
2152+
#else
2153+
dui.includePaths.push_back("./testsuite");
2154+
#endif
2155+
simplecpp::OutputList outputList;
2156+
simplecpp::preprocess(tokens2, rawtokens, files, cache, dui, &outputList);
2157+
ASSERT_EQUALS("", toString(outputList));
2158+
}
2159+
21342160
static void appleFrameworkHasIncludeTest()
21352161
{
21362162
const char code[] =
@@ -3389,6 +3415,7 @@ int main(int argc, char **argv)
33893415
TEST_CASE(nestedInclude);
33903416
TEST_CASE(systemInclude);
33913417
TEST_CASE(circularInclude);
3418+
TEST_CASE(appleFrameworkIncludeTest);
33923419
TEST_CASE(appleFrameworkHasIncludeTest);
33933420

33943421
TEST_CASE(nullDirective1);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Dummy Foundation.h for appleFrameworkIncludeTest

0 commit comments

Comments
 (0)