Skip to content

Commit 6fc70f6

Browse files
committed
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. Co-authored-by: Hans Johnson <hans-johnson@uiowa.edu>
1 parent d85bbd8 commit 6fc70f6

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

simplecpp.cpp

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

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

test.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,32 @@ static void circularInclude()
21412141
ASSERT_EQUALS("", toString(outputList));
21422142
}
21432143

2144+
static void appleFrameworkIncludeTest()
2145+
{
2146+
// This test checks Apple framework include handling.
2147+
//
2148+
// If -I /tmp/testFrameworks
2149+
// and we write:
2150+
// #include <Foundation/Foundation.h>
2151+
//
2152+
// then simplecpp should find:
2153+
// ./testsuite/Foundation.framework/Headers/Foundation.h
2154+
const char code[] = "#include <Foundation/Foundation.h>\n";
2155+
std::vector<std::string> files;
2156+
const simplecpp::TokenList rawtokens = makeTokenList(code, files, "sourcecode.cpp");
2157+
simplecpp::FileDataCache cache;
2158+
simplecpp::TokenList tokens2(files);
2159+
simplecpp::DUI dui;
2160+
#ifdef SIMPLECPP_SOURCE_DIR
2161+
dui.includePaths.push_back(testSourceDir + "/testsuite");
2162+
#else
2163+
dui.includePaths.push_back("./testsuite");
2164+
#endif
2165+
simplecpp::OutputList outputList;
2166+
simplecpp::preprocess(tokens2, rawtokens, files, cache, dui, &outputList);
2167+
ASSERT_EQUALS("", toString(outputList));
2168+
}
2169+
21442170
static void appleFrameworkHasIncludeTest()
21452171
{
21462172
const char code[] =
@@ -3437,6 +3463,7 @@ int main(int argc, char **argv)
34373463
TEST_CASE(nestedInclude);
34383464
TEST_CASE(systemInclude);
34393465
TEST_CASE(circularInclude);
3466+
TEST_CASE(appleFrameworkIncludeTest);
34403467
TEST_CASE(appleFrameworkHasIncludeTest);
34413468

34423469
TEST_CASE(nullDirective1);

0 commit comments

Comments
 (0)