aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Frontend/CompilerInstance.cpp
diff options
context:
space:
mode:
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>2020-12-10 15:27:51 -0800
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>2020-12-23 15:18:50 -0800
commit3ee43adfb20d5dc56b7043b314bd22f457c55483 (patch)
tree53f588c5df51e1ed87cbb3ae42a7ef55b758701a /clang/lib/Frontend/CompilerInstance.cpp
parent245218bb355599771ba43a0fe1449d1670f2666c (diff)
downloadllvm-3ee43adfb20d5dc56b7043b314bd22f457c55483.zip
llvm-3ee43adfb20d5dc56b7043b314bd22f457c55483.tar.gz
llvm-3ee43adfb20d5dc56b7043b314bd22f457c55483.tar.bz2
Basic: Add native support for stdin to SourceManager and FileManager
Add support for stdin to SourceManager and FileManager. Adds FileManager::getSTDIN, which adds a FileEntryRef for `<stdin>` and reads the MemoryBuffer, which is stored as `FileEntry::Content`. Eventually the other buffers in `ContentCache` will sink to here as well -- we probably usually want to load/save a MemoryBuffer eagerly -- but it's happening early for stdin to get rid of CompilerInstance::InitializeSourceManager's final call to `SourceManager::overrideFileContents`. clang/test/CXX/modules-ts/dcl.dcl/dcl.module/dcl.module.export/p1.cpp relies on building a module from stdin; supporting that requires setting ContentCache::BufferOverridden. Differential Revision: https://reviews.llvm.org/D93148
Diffstat (limited to 'clang/lib/Frontend/CompilerInstance.cpp')
-rw-r--r--clang/lib/Frontend/CompilerInstance.cpp35
1 files changed, 12 insertions, 23 deletions
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index d96afae..5c413ec 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -848,33 +848,22 @@ bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input,
StringRef InputFile = Input.getFile();
// Figure out where to get and map in the main file.
- if (InputFile != "-") {
- auto FileOrErr = FileMgr.getFileRef(InputFile, /*OpenFile=*/true);
- if (!FileOrErr) {
- // FIXME: include the error in the diagnostic.
- consumeError(FileOrErr.takeError());
+ auto FileOrErr = InputFile == "-"
+ ? FileMgr.getSTDIN()
+ : FileMgr.getFileRef(InputFile, /*OpenFile=*/true);
+ if (!FileOrErr) {
+ // FIXME: include the error in the diagnostic even when it's not stdin.
+ auto EC = llvm::errorToErrorCode(FileOrErr.takeError());
+ if (InputFile != "-")
Diags.Report(diag::err_fe_error_reading) << InputFile;
- return false;
- }
-
- SourceMgr.setMainFileID(
- SourceMgr.createFileID(*FileOrErr, SourceLocation(), Kind));
- } else {
- llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> SBOrErr =
- llvm::MemoryBuffer::getSTDIN();
- if (std::error_code EC = SBOrErr.getError()) {
+ else
Diags.Report(diag::err_fe_error_reading_stdin) << EC.message();
- return false;
- }
- std::unique_ptr<llvm::MemoryBuffer> SB = std::move(SBOrErr.get());
-
- FileEntryRef File = FileMgr.getVirtualFileRef(SB->getBufferIdentifier(),
- SB->getBufferSize(), 0);
- SourceMgr.setMainFileID(
- SourceMgr.createFileID(File, SourceLocation(), Kind));
- SourceMgr.overrideFileContents(File, std::move(SB));
+ return false;
}
+ SourceMgr.setMainFileID(
+ SourceMgr.createFileID(*FileOrErr, SourceLocation(), Kind));
+
assert(SourceMgr.getMainFileID().isValid() &&
"Couldn't establish MainFileID!");
return true;