[analyzer][NFC] Refactor AnalysisConsumer::getModeForDecl()
authorBalazs Benics <balazs.benics@sigmatechnology.se>
Mon, 29 Nov 2021 09:39:36 +0000 (10:39 +0100)
committerBalazs Benics <balazs.benics@sigmatechnology.se>
Mon, 29 Nov 2021 09:39:36 +0000 (10:39 +0100)
I just read this part of the code, and I found the nested ifs less
readable.

Reviewed By: martong

Differential Revision: https://reviews.llvm.org/D114441

clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp

index 31de49033ac2b72b0f0594273514229565b51d26..f692c68045eebbf3a9168c751eb4b9ad5929d97e 100644 (file)
@@ -591,16 +591,24 @@ AnalysisConsumer::getModeForDecl(Decl *D, AnalysisMode Mode) {
   // - Main source file: run both path-sensitive and non-path-sensitive checks.
   // - Header files: run non-path-sensitive checks only.
   // - System headers: don't run any checks.
-  SourceManager &SM = Ctx->getSourceManager();
-  const Stmt *Body = D->getBody();
-  SourceLocation SL = Body ? Body->getBeginLoc() : D->getLocation();
-  SL = SM.getExpansionLoc(SL);
-
-  if (!Opts->AnalyzeAll && !Mgr->isInCodeFile(SL)) {
-    if (SL.isInvalid() || SM.isInSystemHeader(SL))
-      return AM_None;
+  if (Opts->AnalyzeAll)
+    return Mode;
+
+  const SourceManager &SM = Ctx->getSourceManager();
+
+  const SourceLocation Loc = [&SM](Decl *D) -> SourceLocation {
+    const Stmt *Body = D->getBody();
+    SourceLocation SL = Body ? Body->getBeginLoc() : D->getLocation();
+    return SM.getExpansionLoc(SL);
+  }(D);
+
+  // Ignore system headers.
+  if (Loc.isInvalid() || SM.isInSystemHeader(Loc))
+    return AM_None;
+
+  // Disable path sensitive analysis in user-headers.
+  if (!Mgr->isInCodeFile(Loc))
     return Mode & ~AM_Path;
-  }
 
   return Mode;
 }