diff options
author | Mark Spatz <mark.h.spatz@gmail.com> | 2021-01-19 12:26:34 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-19 12:26:34 -0800 |
commit | 91b0958c43dd3165114b79dedcae48feddd67a81 (patch) | |
tree | 68b8e7b63db2fed9ccb5c6b2fe7276f5cf318ca1 | |
parent | 8008dde117ef165ef115564d58cb95a7d11ac918 (diff) | |
download | flatbuffers-91b0958c43dd3165114b79dedcae48feddd67a81.tar.gz flatbuffers-91b0958c43dd3165114b79dedcae48feddd67a81.tar.bz2 flatbuffers-91b0958c43dd3165114b79dedcae48feddd67a81.zip |
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
-rw-r--r-- | src/idl_parser.cpp | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 0b108534..bf7ff8f4 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); |