From c705d2520a51de1dc38d36efada8e9bc2d8b0d1f Mon Sep 17 00:00:00 2001
From: Argyrios Kyrtzidis <akyrtzi@gmail.com>
Date: Tue, 18 Oct 2011 21:59:54 +0000
Subject: [PATCH] [libclang] Make sure we do a correct invalid check in
 clang_getExpansionLocation.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@142430 91177308-0d34-0410-b5e6-96231b3b80d8
---
 include/clang/Basic/SourceManager.h | 4 ++++
 tools/libclang/CIndex.cpp           | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/include/clang/Basic/SourceManager.h b/include/clang/Basic/SourceManager.h
index 985ddd64127..52b5c4512e2 100644
--- a/include/clang/Basic/SourceManager.h
+++ b/include/clang/Basic/SourceManager.h
@@ -1213,6 +1213,10 @@ public:
   }
 
   const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
+    if (FID.ID == 0 || FID.ID == -1) {
+      if (Invalid) *Invalid = true;
+      return LocalSLocEntryTable[0];
+    }
     return getSLocEntryByID(FID.ID);
   }
 
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp
index e085a27c095..3130a029e7e 100644
--- a/tools/libclang/CIndex.cpp
+++ b/tools/libclang/CIndex.cpp
@@ -2814,7 +2814,7 @@ void clang_getExpansionLocation(CXSourceLocation location,
   FileID fileID = SM.getFileID(ExpansionLoc);
   bool Invalid = false;
   const SrcMgr::SLocEntry &sloc = SM.getSLocEntry(fileID, &Invalid);
-  if (!sloc.isFile() || Invalid) {
+  if (Invalid || !sloc.isFile()) {
     createNullLocation(file, line, column, offset);
     return;
   }
-- 
GitLab