From 91b0958c43dd3165114b79dedcae48feddd67a81 Mon Sep 17 00:00:00 2001 From: Mark Spatz Date: Tue, 19 Jan 2021 12:26:34 -0800 Subject: [PATCH] Search for includes in the directory containg the current file (#6371) * [codegen] Search for includes in the directory containg the current file This matches the behavior of a C preprocessor, see: https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html * Skip FileExists() when file is guaranteed to not exist * end comment with period to match others --- src/idl_parser.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 0b1085347..bf7ff8f4c 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -3176,11 +3176,19 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths, if (opts.proto_mode && attribute_ == "public") NEXT(); auto name = flatbuffers::PosixPath(attribute_.c_str()); EXPECT(kTokenStringConstant); - // Look for the file in include_paths. + // Look for the file relative to the directory of the current file. std::string filepath; - for (auto paths = include_paths; paths && *paths; paths++) { - filepath = flatbuffers::ConCatPathFileName(*paths, name); - if (FileExists(filepath.c_str())) break; + if (source_filename) { + auto source_file_directory = + flatbuffers::StripFileName(source_filename); + filepath = flatbuffers::ConCatPathFileName(source_file_directory, name); + } + if (filepath.empty() || !FileExists(filepath.c_str())) { + // Look for the file in include_paths. + for (auto paths = include_paths; paths && *paths; paths++) { + filepath = flatbuffers::ConCatPathFileName(*paths, name); + if (FileExists(filepath.c_str())) break; + } } if (filepath.empty()) return Error("unable to locate include file: " + name);