]> granicus.if.org Git - clang/commitdiff
Split isFromMainFile into two functions.
authorEli Friedman <eli.friedman@gmail.com>
Thu, 22 Aug 2013 00:27:10 +0000 (00:27 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Thu, 22 Aug 2013 00:27:10 +0000 (00:27 +0000)
Basically, isInMainFile considers line markers, and isWrittenInMainFile
doesn't.  Distinguishing between the two is useful when dealing with
files which are preprocessed files or rewritten with -frewrite-includes
(so we don't, for example, print useless warnings).

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

19 files changed:
include/clang/Basic/SourceManager.h
lib/Frontend/VerifyDiagnosticConsumer.cpp
lib/Lex/PPDirectives.cpp
lib/Rewrite/Frontend/RewriteMacros.cpp
lib/Rewrite/Frontend/RewriteModernObjC.cpp
lib/Rewrite/Frontend/RewriteObjC.cpp
lib/Sema/Sema.cpp
lib/Sema/SemaChecking.cpp
lib/Sema/SemaDeclCXX.cpp
lib/Sema/SemaExpr.cpp
lib/StaticAnalyzer/Core/BugReporter.cpp
lib/StaticAnalyzer/Core/CallEvent.cpp
lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp
lib/StaticAnalyzer/Core/PathDiagnostic.cpp
lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
test/Preprocessor/warn-macro-unused.c
test/Sema/inline.c
test/SemaCXX/warn-using-namespace-in-header.cpp
tools/libclang/CXSourceLocation.cpp

index 9fc48852205db80738fcf7f6529a525829a76307..c2bf2c5da01e1750fea443ee2a64d048d6004aa2 100644 (file)
@@ -1292,14 +1292,30 @@ public:
   PresumedLoc getPresumedLoc(SourceLocation Loc,
                              bool UseLineDirectives = true) const;
 
-  /// \brief Returns true if both SourceLocations correspond to the same file.
-  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
+  /// \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 isInMainFile(SourceLocation Loc) const {
+    return getPresumedLoc(Loc).getIncludeLoc().isInvalid();
+  }
+
+  /// \brief Returns true if the spelling locations for both SourceLocations
+  /// are part of the same file buffer.
+  ///
+  /// This check ignores line marker directives.
+  bool isWrittenInSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
     return getFileID(Loc1) == getFileID(Loc2);
   }
 
-  /// \brief Returns true if the file of provided SourceLocation is the main
-  /// file.
-  bool isFromMainFile(SourceLocation Loc) const {
+  /// \brief Returns true if the spelling location for the given location
+  /// is in the main file buffer.
+  ///
+  /// This check ignores line marker directives.
+  bool isWrittenInMainFile(SourceLocation Loc) const {
     return getFileID(Loc) == getMainFileID();
   }
 
index efe6005ed7848f12178662414492178ad980bf19..f5555291a831fde0fde1bb27ad4989116f6f6abe 100644 (file)
@@ -628,11 +628,11 @@ static bool IsFromSameFile(SourceManager &SM, SourceLocation DirectiveLoc,
   while (DiagnosticLoc.isMacroID())
     DiagnosticLoc = SM.getImmediateMacroCallerLoc(DiagnosticLoc);
 
-  if (SM.isFromSameFile(DirectiveLoc, DiagnosticLoc))
+  if (SM.isWrittenInSameFile(DirectiveLoc, DiagnosticLoc))
     return true;
 
   const FileEntry *DiagFile = SM.getFileEntryForID(SM.getFileID(DiagnosticLoc));
-  if (!DiagFile && SM.isFromMainFile(DirectiveLoc))
+  if (!DiagFile && SM.isWrittenInMainFile(DirectiveLoc))
     return true;
 
   return (DiagFile == SM.getFileEntryForID(SM.getFileID(DirectiveLoc)));
index ef0cbd656f9ce411be2d4f8c1a059a8281400597..86d6ad9eb868e495b8c0ef54dd9c473bed0c73e3 100644 (file)
@@ -2040,7 +2040,7 @@ void Preprocessor::HandleDefineDirective(Token &DefineTok,
   assert(!MI->isUsed());
   // If we need warning for not using the macro, add its location in the
   // warn-because-unused-macro set. If it gets used it will be removed from set.
-  if (isInPrimaryFile() && // don't warn for include'd macros.
+  if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
       Diags->getDiagnosticLevel(diag::pp_macro_not_used,
           MI->getDefinitionLoc()) != DiagnosticsEngine::Ignored) {
     MI->setIsWarnIfUnused(true);
index 3c1d2e11903d5e36adcfcbcdfc78e9a4efc069ff..4f6a93f7e0eb15e8dbba5baea6697ed08cc20eb8 100644 (file)
@@ -115,7 +115,7 @@ void clang::RewriteMacrosInInput(Preprocessor &PP, raw_ostream *OS) {
     SourceLocation PPLoc = SM.getExpansionLoc(PPTok.getLocation());
 
     // If PPTok is from a different source file, ignore it.
-    if (!SM.isFromMainFile(PPLoc)) {
+    if (!SM.isWrittenInMainFile(PPLoc)) {
       PP.Lex(PPTok);
       continue;
     }
index 546f9217ae16fd5c046ba64ea92be99a76e6e883..80b6b4110cd13f25dbb6e26517429956eb687d5f 100644 (file)
@@ -791,7 +791,7 @@ void RewriteModernObjC::HandleTopLevelSingleDecl(Decl *D) {
     }
   }
   // If we have a decl in the main file, see if we should rewrite it.
-  if (SM->isFromMainFile(Loc))
+  if (SM->isWrittenInMainFile(Loc))
     return HandleDeclInMainFile(D);
 }
 
index 316009fc51aac6b82c3f16cf8590e3ba98a88517..d265c51f5b0b51826aa0c9e0afcf64901ab1589d 100644 (file)
@@ -721,7 +721,7 @@ void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) {
     }
   }
   // If we have a decl in the main file, see if we should rewrite it.
-  if (SM->isFromMainFile(Loc))
+  if (SM->isWrittenInMainFile(Loc))
     return HandleDeclInMainFile(D);
 }
 
index 26f26316d1d09f5603816e2f1651baed9177ed1d..6cc596cec1b8d6fbedba65830f2aecb0557fa6b7 100644 (file)
@@ -722,7 +722,7 @@ void Sema::ActOnEndOfTranslationUnit() {
           else {
             if (FD->getStorageClass() == SC_Static &&
                 !FD->isInlineSpecified() &&
-                !SourceMgr.isFromMainFile(
+                !SourceMgr.isInMainFile(
                    SourceMgr.getExpansionLoc(FD->getLocation())))
               Diag(DiagD->getLocation(), diag::warn_unneeded_static_internal_decl)
                 << DiagD->getDeclName();
@@ -743,7 +743,7 @@ void Sema::ActOnEndOfTranslationUnit() {
         if (DiagD->isReferenced()) {
           Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
                 << /*variable*/1 << DiagD->getDeclName();
-        } else if (getSourceManager().isFromMainFile(DiagD->getLocation())) {
+        } else if (SourceMgr.isInMainFile(DiagD->getLocation())) {
           // If the declaration is in a header which is included into multiple
           // TUs, it will declare one variable per TU, and one of the other
           // variables may be used. So, only warn if the declaration is in the
index 6fd3aa1fd4fbd1c8dc8c20f40cb4a1c4c585ba16..5e0acc078755c9853e050ec8a803fb4f5948aef3 100644 (file)
@@ -6288,7 +6288,7 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
       if (SourceMgr.isInSystemHeader(RBracketLoc)) {
         SourceLocation IndexLoc = SourceMgr.getSpellingLoc(
             IndexExpr->getLocStart());
-        if (SourceMgr.isFromSameFile(RBracketLoc, IndexLoc))
+        if (SourceMgr.isWrittenInSameFile(RBracketLoc, IndexLoc))
           return;
       }
     }
index 528396d5598a4e77d3b8832c6eeec1d5993e3918..6ebfb57974e7f39c20c8dd849cd58b744b062734 100644 (file)
@@ -6710,7 +6710,7 @@ Decl *Sema::ActOnUsingDirective(Scope *S,
                                       IdentLoc, Named, CommonAncestor);
 
     if (IsUsingDirectiveInToplevelContext(CurContext) &&
-        !SourceMgr.isFromMainFile(SourceMgr.getExpansionLoc(IdentLoc))) {
+        !SourceMgr.isInMainFile(SourceMgr.getExpansionLoc(IdentLoc))) {
       Diag(IdentLoc, diag::warn_using_directive_in_header);
     }
 
index 2dbc82dc7a537dd5c84c2221f811d3f2decff12d..1c5e24e278f3bf7f4f902d2af598cf9048463487 100644 (file)
@@ -229,7 +229,7 @@ static void diagnoseUseOfInternalDeclInInlineFunction(Sema &S,
   // This last can give us false negatives, but it's better than warning on
   // wrappers for simple C library functions.
   const FunctionDecl *UsedFn = dyn_cast<FunctionDecl>(D);
-  bool DowngradeWarning = S.getSourceManager().isFromMainFile(Loc);
+  bool DowngradeWarning = S.getSourceManager().isInMainFile(Loc);
   if (!DowngradeWarning && UsedFn)
     DowngradeWarning = UsedFn->isInlined() || UsedFn->hasAttr<ConstAttr>();
 
index 2123a4518114ff5610c261c40fc5bb0a61a742be..1df3053edfc24b6be4397a3cf3eca5f57b7c4ffc 100644 (file)
@@ -2233,7 +2233,7 @@ static void removePunyEdges(PathPieces &path,
     SourceLocation FirstLoc = start->getLocStart();
     SourceLocation SecondLoc = end->getLocStart();
 
-    if (!SM.isFromSameFile(FirstLoc, SecondLoc))
+    if (!SM.isWrittenInSameFile(FirstLoc, SecondLoc))
       continue;
     if (SM.isBeforeInTranslationUnit(SecondLoc, FirstLoc))
       std::swap(SecondLoc, FirstLoc);
index 9ab687475618391837f82ad6deaa6cb3ce7d3c23..59cdfbb7f9e30819105319d9af088f0a251025e6 100644 (file)
@@ -741,7 +741,7 @@ bool ObjCMethodCall::canBeOverridenInSubclass(ObjCInterfaceDecl *IDecl,
   // TODO: It could actually be subclassed if the subclass is private as well.
   // This is probably very rare.
   SourceLocation InterfLoc = IDecl->getEndOfDefinitionLoc();
-  if (InterfLoc.isValid() && SM.isFromMainFile(InterfLoc))
+  if (InterfLoc.isValid() && SM.isInMainFile(InterfLoc))
     return false;
 
   // Assume that property accessors are not overridden.
@@ -763,7 +763,7 @@ bool ObjCMethodCall::canBeOverridenInSubclass(ObjCInterfaceDecl *IDecl,
       return false;
 
     // If outside the main file,
-    if (D->getLocation().isValid() && !SM.isFromMainFile(D->getLocation()))
+    if (D->getLocation().isValid() && !SM.isInMainFile(D->getLocation()))
       return true;
 
     if (D->isOverriding()) {
index 63dccff374bcc8f9d0019d26078eaa7b452bf1c6..4fb6523944ff95aa69684feeaef80f88048eb934 100644 (file)
@@ -764,7 +764,7 @@ static bool mayInlineDecl(const CallEvent &Call, AnalysisDeclContext *CalleeADC,
       // Conditionally control the inlining of methods on objects that look
       // like C++ containers.
       if (!Opts.mayInlineCXXContainerCtorsAndDtors())
-        if (!Ctx.getSourceManager().isFromMainFile(FD->getLocation()))
+        if (!Ctx.getSourceManager().isInMainFile(FD->getLocation()))
           if (isContainerCtorOrDtor(Ctx, FD))
             return false;
             
index 82d8f37105aa4a6c8ed3ce37c42c2cd339e2316d..5f26085cabeb6a69f651a6307bce5f3cc4370306 100644 (file)
@@ -129,11 +129,11 @@ getFirstStackedCallToHeaderFile(PathDiagnosticCallPiece *CP,
   if (CallLoc.isMacroID())
     return 0;
 
-  assert(SMgr.isFromMainFile(CallLoc) &&
+  assert(SMgr.isInMainFile(CallLoc) &&
          "The call piece should be in the main file.");
 
   // Check if CP represents a path through a function outside of the main file.
-  if (!SMgr.isFromMainFile(CP->callEnterWithin.asLocation()))
+  if (!SMgr.isInMainFile(CP->callEnterWithin.asLocation()))
     return CP;
 
   const PathPieces &Path = CP->path;
index abc1e9fc529978dfe0b818b420d182258fe47198..9efe99767e4c9e1a48f78dae56662ae505667ad1 100644 (file)
@@ -596,7 +596,7 @@ AnalysisConsumer::getModeForDecl(Decl *D, AnalysisMode Mode) {
   // - System headers: don't run any checks.
   SourceManager &SM = Ctx->getSourceManager();
   SourceLocation SL = SM.getExpansionLoc(D->getLocation());
-  if (!Opts->AnalyzeAll && !SM.isFromMainFile(SL)) {
+  if (!Opts->AnalyzeAll && !SM.isInMainFile(SL)) {
     if (SL.isInvalid() || SM.isInSystemHeader(SL))
       return AM_None;
     return Mode & ~AM_Path;
index c33aeb5df9d9f282e4eb972ea6877af3bca6a9ab..a305cc9966ad8f526f2213f347774326582e8491 100644 (file)
@@ -2,6 +2,10 @@
 
 #include "warn-macro-unused.h"
 
+# 1 "warn-macro-unused-fake-header.h" 1
+#define unused_from_fake_header
+# 5 "warn-macro-unused.c" 2
+
 #define unused // expected-warning {{macro is not used}}
 #define unused
 unused
index 496e282ecacdd0729a431c0ebeed41b0c67191fb..8a3835b71ada3cda0daa06ae3c3e1e2558999228 100644 (file)
@@ -83,6 +83,19 @@ extern inline void defineStaticVarInExtern() {
   static int y = 0; // ok
 }
 
+// Check behavior of line markers.
+# 1 "XXX.h" 1
+inline int useStaticMainFileInLineMarker() { // expected-note 2 {{use 'static' to give inline function 'useStaticMainFileInLineMarker' internal linkage}}
+  staticFunction(); // expected-warning{{static function 'staticFunction' is used in an inline function with external linkage}}
+  return staticVar; // expected-warning{{static variable 'staticVar' is used in an inline function with external linkage}}
+}
+# 100 "inline.c" 2
+
+inline int useStaticMainFileAfterLineMarker() {
+  staticFunction(); // no-warning
+  return staticVar; // no-warning
+}
+
 #endif
 
 
index f68b99893aae7d7dee6069bcd9aefa97ec1c07cf..6b84e6e70a507d791f7251e6d9075c4f68f6b6b4 100644 (file)
@@ -57,4 +57,13 @@ using namespace dont_warn;
 // cc file.
 USING_MACRO
 
+// Check behavior of line markers.
+namespace warn_header_with_line_marker {} 
+# 1 "XXX.h" 1
+using namespace warn_header_with_line_marker; // expected-warning {{using namespace directive in global context in header}}
+# 70 "warn-using-namespace-in-header.cpp" 2
+
+namespace nowarn_after_line_marker {}
+using namespace nowarn_after_line_marker;
+
 #endif
index 71e425d1f7f38bd7bbd6a167350a7c64e1e5e5e7..a33c9d4f26995fd86af7b3382387205fafe3a9d2 100644 (file)
@@ -217,7 +217,7 @@ int clang_Location_isFromMainFile(CXSourceLocation location) {
 
   const SourceManager &SM =
     *static_cast<const SourceManager*>(location.ptr_data[0]);
-  return SM.isFromMainFile(Loc);
+  return SM.isWrittenInMainFile(Loc);
 }
 
 void clang_getExpansionLocation(CXSourceLocation location,