aboutsummaryrefslogtreecommitdiff
path: root/flang/lib/Parser/preprocessor.cpp
diff options
context:
space:
mode:
authorpeter klausler <pklausler@nvidia.com>2021-01-25 12:29:10 -0800
committerpeter klausler <pklausler@nvidia.com>2021-01-25 13:39:37 -0800
commitd987b61b1dce9948801ac37704477e7c257100b1 (patch)
tree6a8a54ab211362a19b5eb575034a05a2fb5789a7 /flang/lib/Parser/preprocessor.cpp
parentf50b8ee71faeb5056df7a950e7427f3047ff9987 (diff)
downloadllvm-d987b61b1dce9948801ac37704477e7c257100b1.zip
llvm-d987b61b1dce9948801ac37704477e7c257100b1.tar.gz
llvm-d987b61b1dce9948801ac37704477e7c257100b1.tar.bz2
[flang] Search for #include "file" in right directory
Make the #include "file" preprocessing directive begin its search in the same directory as the file containing the directive, as other preprocessors and our Fortran INCLUDE statement do. Avoid current working directory for all source files after the original. Differential Revision: https://reviews.llvm.org/D95388
Diffstat (limited to 'flang/lib/Parser/preprocessor.cpp')
-rw-r--r--flang/lib/Parser/preprocessor.cpp10
1 files changed, 9 insertions, 1 deletions
diff --git a/flang/lib/Parser/preprocessor.cpp b/flang/lib/Parser/preprocessor.cpp
index c5422cc..14c9e54 100644
--- a/flang/lib/Parser/preprocessor.cpp
+++ b/flang/lib/Parser/preprocessor.cpp
@@ -399,6 +399,7 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner *prescanner) {
if (j == tokens) {
return;
}
+ CHECK(prescanner); // TODO: change to reference
if (dir.TokenAt(j).ToString() != "#") {
prescanner->Say(dir.GetTokenProvenanceRange(j), "missing '#'"_err_en_US);
return;
@@ -578,6 +579,7 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner *prescanner) {
return;
}
std::string include;
+ std::optional<std::string> prependPath;
if (dir.TokenAt(j).ToString() == "<") { // #include <foo>
std::size_t k{j + 1};
if (k >= tokens) {
@@ -598,6 +600,12 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner *prescanner) {
} else if ((include = dir.TokenAt(j).ToString()).substr(0, 1) == "\"" &&
include.substr(include.size() - 1, 1) == "\"") { // #include "foo"
include = include.substr(1, include.size() - 2);
+ // #include "foo" starts search in directory of file containing
+ // the directive
+ auto prov{dir.GetTokenProvenanceRange(dirOffset).start()};
+ if (const auto *currentFile{allSources_.GetSourceFile(prov)}) {
+ prependPath = DirectoryName(currentFile->path());
+ }
} else {
prescanner->Say(dir.GetTokenProvenanceRange(j < tokens ? j : tokens - 1),
"#include: expected name of file to include"_err_en_US);
@@ -615,7 +623,7 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner *prescanner) {
}
std::string buf;
llvm::raw_string_ostream error{buf};
- const SourceFile *included{allSources_.Open(include, error)};
+ const SourceFile *included{allSources_.Open(include, error, prependPath)};
if (!included) {
prescanner->Say(dir.GetTokenProvenanceRange(dirOffset),
"#include: %s"_err_en_US, error.str());