Skip to content

Commit 56d2c7c

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. Co-authored-by: Hans Johnson <hans-johnson@uiowa.edu>
1 parent 81dad8f commit 56d2c7c

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

simplecpp.cpp

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

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

0 commit comments

Comments
 (0)