]> granicus.if.org Git - clang/commitdiff
SourceManager: Open code isInMainFile.
authorBenjamin Kramer <benny.kra@googlemail.com>
Fri, 27 Sep 2013 17:12:50 +0000 (17:12 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Fri, 27 Sep 2013 17:12:50 +0000 (17:12 +0000)
- We really shouldn't compute line numbers for every file that is asked if it's
  the main file, it destroys the lazy computation.
- Invalid locations are no longer accounted to the main file, no other
  functionality change.

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

include/clang/Basic/SourceManager.h
lib/Basic/SourceManager.cpp

index c2bf2c5da01e1750fea443ee2a64d048d6004aa2..6aab998bd89848cbcee7746232478bf025589dbb 100644 (file)
@@ -1299,9 +1299,7 @@ public:
   /// whether it came from a file other than the main file. This is different
   /// from isWrittenInMainFile() because it takes line marker directives into
   /// account.
-  bool isInMainFile(SourceLocation Loc) const {
-    return getPresumedLoc(Loc).getIncludeLoc().isInvalid();
-  }
+  bool isInMainFile(SourceLocation Loc) const;
 
   /// \brief Returns true if the spelling locations for both SourceLocations
   /// are part of the same file buffer.
index fca0dba9906861f8a366863827efc1437f2e8c79..93e9a594e78b03239f4a4858bd0d96426bc47025 100644 (file)
@@ -1553,6 +1553,36 @@ PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc,
   return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
 }
 
+/// \brief Returns whether the PresumedLoc for a given SourceLocation is
+/// in the main file.
+///
+/// This computes the "presumed" location for a SourceLocation, then checks
+/// whether it came from a file other than the main file. This is different
+/// from isWrittenInMainFile() because it takes line marker directives into
+/// account.
+bool SourceManager::isInMainFile(SourceLocation Loc) const {
+  if (Loc.isInvalid()) return false;
+
+  // Presumed locations are always for expansion points.
+  std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
+
+  bool Invalid = false;
+  const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid);
+  if (Invalid || !Entry.isFile())
+    return false;
+
+  const SrcMgr::FileInfo &FI = Entry.getFile();
+
+  // Check if there is a line directive for this location.
+  if (FI.hasLineDirectives())
+    if (const LineEntry *Entry =
+            LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second))
+      if (Entry->IncludeOffset)
+        return false;
+
+  return FI.getIncludeLoc().isInvalid();
+}
+
 /// \brief The size of the SLocEnty that \arg FID represents.
 unsigned SourceManager::getFileIDSize(FileID FID) const {
   bool Invalid = false;