r319707 - Correctly handle line table entries without filenames during AST serialization

Hans Wennborg via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 4 14:28:45 PST 2017


Author: hans
Date: Mon Dec  4 14:28:45 2017
New Revision: 319707

URL: http://llvm.org/viewvc/llvm-project?rev=319707&view=rev
Log:
Correctly handle line table entries without filenames during AST serialization

The current code would hit an assert in ASTWriter when trying to write
out the filename for a line table entry that didn't have any. Fix this
by allowing the -1 sentinel value to round-trip through serialization.

Differential revision: https://reviews.llvm.org/D40746

Added:
    cfe/trunk/test/PCH/line-directive-nofilename.c
    cfe/trunk/test/PCH/line-directive-nofilename.h
Modified:
    cfe/trunk/lib/Serialization/ASTReader.cpp
    cfe/trunk/lib/Serialization/ASTWriter.cpp

Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=319707&r1=319706&r2=319707&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Mon Dec  4 14:28:45 2017
@@ -1220,6 +1220,7 @@ bool ASTReader::ParseLineTable(ModuleFil
 
   // Parse the file names
   std::map<int, int> FileIDs;
+  FileIDs[-1] = -1; // For unspecified filenames.
   for (unsigned I = 0; Record[Idx]; ++I) {
     // Extract the file name
     auto Filename = ReadPath(F, Record, Idx);

Modified: cfe/trunk/lib/Serialization/ASTWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriter.cpp?rev=319707&r1=319706&r2=319707&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriter.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriter.cpp Mon Dec  4 14:28:45 2017
@@ -2363,12 +2363,13 @@ void ASTWriter::WriteSourceManagerBlock(
 
     // Emit the needed file names.
     llvm::DenseMap<int, int> FilenameMap;
+    FilenameMap[-1] = -1; // For unspecified filenames.
     for (const auto &L : LineTable) {
       if (L.first.ID < 0)
         continue;
       for (auto &LE : L.second) {
         if (FilenameMap.insert(std::make_pair(LE.FilenameID,
-                                              FilenameMap.size())).second)
+                                              FilenameMap.size() - 1)).second)
           AddPath(LineTable.getFilename(LE.FilenameID), Record);
       }
     }

Added: cfe/trunk/test/PCH/line-directive-nofilename.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/line-directive-nofilename.c?rev=319707&view=auto
==============================================================================
--- cfe/trunk/test/PCH/line-directive-nofilename.c (added)
+++ cfe/trunk/test/PCH/line-directive-nofilename.c Mon Dec  4 14:28:45 2017
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -emit-pch -o %t %S/line-directive-nofilename.h
+// RUN: not %clang_cc1 -include-pch %t -fsyntax-only %s 2>&1 | FileCheck %s
+
+// This causes an "error: redefinition" diagnostic. The notes will have the
+// locations of the declarations from the PCH file.
+double foo, bar;
+
+// CHECK: line-directive-nofilename.h:42:5: note: previous definition is here
+// CHECK: foobar.h:100:5: note: previous definition is here

Added: cfe/trunk/test/PCH/line-directive-nofilename.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/line-directive-nofilename.h?rev=319707&view=auto
==============================================================================
--- cfe/trunk/test/PCH/line-directive-nofilename.h (added)
+++ cfe/trunk/test/PCH/line-directive-nofilename.h Mon Dec  4 14:28:45 2017
@@ -0,0 +1,5 @@
+#line 42
+int foo; // This should appear as at line-directive-nofilename.h:42
+
+#line 100 "foobar.h"
+int bar; // This should appear as at foobar.h:100




More information about the cfe-commits mailing list