================
@@ -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
----------------
cor3ntin wrote:

No, in all cases, we should emit an error, and fail.
There should not be any fallback (if you don't know what encoding your file is 
in... sorry, not sorry). But equally, asserting is not a valid strategy either

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