Introduce a new PresumedLoc class to represent the concept of a location
as reported to the user and as manipulated by #line.  This is what __FILE__,
__INCLUDE_LEVEL__, diagnostics and other things should follow (but not 
dependency generation!).  

This patch also includes several cleanups along the way: 

- SourceLocation now has a dump method, and several other places 
  that did similar things now use it.
- I cleaned up some code in AnalysisConsumer, but it should probably be
  simplified further now that NamedDecl is better.
- TextDiagnosticPrinter is now simplified and cleaned up a bit.

This patch is a prerequisite for #line, but does not actually provide 
any #line functionality.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63098 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Driver/AnalysisConsumer.cpp b/Driver/AnalysisConsumer.cpp
index 4dab9c7..171ffbf 100644
--- a/Driver/AnalysisConsumer.cpp
+++ b/Driver/AnalysisConsumer.cpp
@@ -257,18 +257,14 @@
       
       DisplayedFunction = true;
       
-      if (FunctionDecl *FD = dyn_cast<FunctionDecl>(getCodeDecl())) {
+      // FIXME: Is getCodeDecl() always a named decl?
+      if (isa<FunctionDecl>(getCodeDecl()) ||
+          isa<ObjCMethodDecl>(getCodeDecl())) {
+        NamedDecl *ND = cast<NamedDecl>(getCodeDecl());
+        SourceManager &SM = getContext().getSourceManager();
         llvm::cerr << "ANALYZE: "
-        << getContext().getSourceManager().getSourceName(FD->getLocation())
-        << ' '
-        << FD->getIdentifier()->getName()
-        << '\n';
-      }
-      else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(getCodeDecl())) {
-        llvm::cerr << "ANALYZE (ObjC Method): "
-        << getContext().getSourceManager().getSourceName(MD->getLocation())
-        << " '"
-        << MD->getSelector().getAsString() << "'\n";
+          << SM.getPresumedLoc(ND->getLocation()).getFilename()
+          << ' ' << ND->getNameAsString() << '\n';
       }
     }
 
diff --git a/Driver/DependencyFile.cpp b/Driver/DependencyFile.cpp
index 282164f..0d4ee91 100644
--- a/Driver/DependencyFile.cpp
+++ b/Driver/DependencyFile.cpp
@@ -13,6 +13,7 @@
 
 #include "clang.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Basic/FileManager.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/DirectoryLookup.h"
@@ -167,8 +168,14 @@
                                          SrcMgr::CharacteristicKind FileType) {
   if (Reason != PPCallbacks::EnterFile)
     return;
-
-  const char *Filename = PP->getSourceManager().getSourceName(Loc);
+  
+  // Depedency generation really does want to go all the way to the file entry
+  // for a source location to find out what is depended on.  We do not want
+  // #line markers to affect dependency generation!
+  SourceManager &SM = PP->getSourceManager();
+  
+  FileID FID = SM.getFileID(SM.getInstantiationLoc(Loc));
+  const char *Filename = SM.getFileEntryForID(FID)->getName();
   if (!FileMatchesDepCriteria(Filename, FileType))
     return;
 
diff --git a/Driver/PrintPreprocessedOutput.cpp b/Driver/PrintPreprocessedOutput.cpp
index 32b8a8b..5c4a140 100644
--- a/Driver/PrintPreprocessedOutput.cpp
+++ b/Driver/PrintPreprocessedOutput.cpp
@@ -150,7 +150,7 @@
   // #include directive was at.
   SourceManager &SourceMgr = PP.getSourceManager();
   if (Reason == PPCallbacks::EnterFile) {
-    MoveToLine(SourceMgr.getIncludeLoc(Loc));
+    MoveToLine(SourceMgr.getPresumedLoc(Loc).getIncludeLoc());
   } else if (Reason == PPCallbacks::SystemHeaderPragma) {
     MoveToLine(Loc);
     
@@ -165,7 +165,7 @@
   if (DisableLineMarkers) return;
 
   CurFilename.clear();
-  CurFilename += SourceMgr.getSourceName(Loc);
+  CurFilename += SourceMgr.getPresumedLoc(Loc).getFilename();
   Lexer::Stringify(CurFilename);
   FileType = NewFileType;
 
@@ -540,7 +540,8 @@
   const SourceManager &SourceMgr = PP.getSourceManager();
   do PP.Lex(Tok);
   while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() &&
-         !strcmp(SourceMgr.getSourceName(Tok.getLocation()), "<predefines>"));
+         !strcmp(SourceMgr.getPresumedLoc(Tok.getLocation()).getFilename(),
+                 "<predefines>"));
 
   while (1) {