================
@@ -152,6 +164,83 @@ ContentCache::getBufferOrNone(DiagnosticsEngine &Diag, 
FileManager &FM,
 
   Buffer = std::move(*BufferOrError);
 
+  // Unless this is a named pipe (in which case we can handle a mismatch),
+  // check that the file's size is the same as in the file entry (which may
+  // have come from a stat cache).
+  assert(Buffer->getBufferSize() >= (size_t)ContentsEntry->getSize());
+  if (!ContentsEntry->isNamedPipe() &&
+      Buffer->getBufferSize() < (size_t)ContentsEntry->getSize()) {
+    Diag.Report(Loc, diag::err_file_modified) << ContentsEntry->getName();
+
+    return std::nullopt;
+  }
+
+  // Convert source from the input charset to UTF-8 if necessary.
+  llvm::TextEncodingConverter *Converter = FileIDConverterInfo.getPointer();
+  if (Converter) {
+    StringRef OriginalBuf = Buffer->getBuffer();
+    llvm::SmallString<0> UTF8Buf;
+    UTF8Buf.reserve(OriginalBuf.size() + 1);
+
+    std::error_code EC = Converter->convert(OriginalBuf, UTF8Buf);
+    if (EC) {
+      // For tagged files, conversion failure is an error and we don't fall 
back
+      if (isFileTagged()) {
+        Diag.Report(Loc, diag::err_encoding_conversion_failed)
+            << ContentsEntry->getName() << EC.message();
+        return std::nullopt;
+      }
+
+      // Only fall back to the default encoding if the file is untagged.
+      // If conversion fails, emit a warning and attempt to fall back to the
+      // default encoding.
+      //
+      // This allows the compiler to accept system or third-party headers that
+      // are encoded in the default encoding even if conversion to the
+      // option-specified input encoding failed.
+      //
+      // TODO: Add input byte offset information.
+      // FIXME: Need to fallback to IBM-1047 if that is the default even if the
----------------
hubert-reinterpretcast wrote:

> But equally, asserting is not a valid strategy either

Within the scope of this PR, conversion only occurs from IBM-1047. That 
conversion never fails and, therefore, the assertion consistent.

https://github.com/llvm/llvm-project/pull/206519
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to