]> granicus.if.org Git - clang/commitdiff
Don't crash if we can't find FileEntry info for a typedef, since one
authorEli Friedman <eli.friedman@gmail.com>
Wed, 11 Jun 2008 06:20:39 +0000 (06:20 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Wed, 11 Jun 2008 06:20:39 +0000 (06:20 +0000)
isn't guaranteed to exist. This fixes a crash with conflicting typedefs
coming from stdin.

This also fixes the crash in PR2406, but doesn't completely fix the
issue; it appears there's something strange about the physical location
for the definition of int64_t in stdlib.h.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@52209 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/Sema/typedef-redef.c [new file with mode: 0644]

index 2d6422fb0071b87e1324fe9f11bf4c09e8a37972..0bab523043cb7bda149f15d79ba8703c0ee9413c 100644 (file)
@@ -230,28 +230,31 @@ TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
   // FIXME: Verify the underlying types are equivalent!
   if (getLangOptions().ObjC1 && isBuiltinObjCType(New))
     return Old;
-  
+
+  if (getLangOptions().Microsoft) return New;
+
   // Redeclaration of a type is a constraint violation (6.7.2.3p1).
   // Apparently GCC, Intel, and Sun all silently ignore the redeclaration if
   // *either* declaration is in a system header. The code below implements
   // this adhoc compatibility rule. FIXME: The following code will not
   // work properly when compiling ".i" files (containing preprocessed output).
   SourceManager &SrcMgr = Context.getSourceManager();
+  HeaderSearch &HdrInfo = PP.getHeaderSearchInfo();
   const FileEntry *OldDeclFile = SrcMgr.getFileEntryForLoc(Old->getLocation());
+  if (OldDeclFile) {
+    DirectoryLookup::DirType OldDirType = HdrInfo.getFileDirFlavor(OldDeclFile);
+    // Allow reclarations in both SystemHeaderDir and ExternCSystemHeaderDir.
+    if (OldDirType != DirectoryLookup::NormalHeaderDir)
+      return New;
+  }
   const FileEntry *NewDeclFile = SrcMgr.getFileEntryForLoc(New->getLocation());
-  HeaderSearch &HdrInfo = PP.getHeaderSearchInfo();
-  DirectoryLookup::DirType OldDirType = HdrInfo.getFileDirFlavor(OldDeclFile);
-  DirectoryLookup::DirType NewDirType = HdrInfo.getFileDirFlavor(NewDeclFile);
-  
-  // Allow reclarations in both SystemHeaderDir and ExternCSystemHeaderDir.
-  if ((OldDirType != DirectoryLookup::NormalHeaderDir ||
-       NewDirType != DirectoryLookup::NormalHeaderDir) ||
-      getLangOptions().Microsoft)
-    return New;
-      
-  // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
-  // TODO: This is totally simplistic.  It should handle merging functions
-  // together etc, merging extern int X; int X; ...
+  if (NewDeclFile) {
+    DirectoryLookup::DirType NewDirType = HdrInfo.getFileDirFlavor(NewDeclFile);
+    // Allow reclarations in both SystemHeaderDir and ExternCSystemHeaderDir.
+    if (NewDirType != DirectoryLookup::NormalHeaderDir)
+      return New;
+  }
+
   Diag(New->getLocation(), diag::err_redefinition, New->getName());
   Diag(Old->getLocation(), diag::err_previous_definition);
   return New;
diff --git a/test/Sema/typedef-redef.c b/test/Sema/typedef-redef.c
new file mode 100644 (file)
index 0000000..1b12ae4
--- /dev/null
@@ -0,0 +1,4 @@
+// RUN: clang < %s -fsyntax-only -verify
+
+#include <stddef.h>
+typedef unsigned long size_t;