[cfe-commits] r148516 - in /cfe/trunk: include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h lib/StaticAnalyzer/Core/CheckerContext.cpp

David Blaikie dblaikie at gmail.com
Fri Jan 20 12:55:24 PST 2012


On Thu, Jan 19, 2012 at 4:11 PM, Anna Zaks <ganna at apple.com> wrote:
> Author: zaks
> Date: Thu Jan 19 18:11:12 2012
> New Revision: 148516
>
> URL: http://llvm.org/viewvc/llvm-project?rev=148516&view=rev
> Log:
> [analyzer] Add a utility method that allows to find the macro name used
> at the given location.
>
> This could be useful when checkers' logic depends on whether a function
> is called with a given macro argument.
>
> Modified:
>    cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
>    cfe/trunk/lib/StaticAnalyzer/Core/CheckerContext.cpp
>
> Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h?rev=148516&r1=148515&r2=148516&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h (original)
> +++ cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h Thu Jan 19 18:11:12 2012
> @@ -66,7 +66,11 @@
>   ASTContext &getASTContext() {
>     return Eng.getContext();
>   }
> -
> +
> +  const LangOptions &getLangOptions() const {
> +    return Eng.getContext().getLangOptions();
> +  }
> +
>   const LocationContext *getLocationContext() const {
>     return Pred->getLocationContext();
>   }
> @@ -161,6 +165,17 @@
>   /// function with the given name.
>   bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name);
>
> +  /// \brief Depending on wither the location corresponds to a macro, return
> +  /// either the macro name or the token spelling.
> +  ///
> +  /// This could be useful when checkers' logic depends on whether a function
> +  /// is called with a given macro argument. For example:
> +  ///   s = socket(AF_INET,..)
> +  /// If AF_INET is a macro, the result should be treated as a source of taint.
> +  ///
> +  /// \sa clang::Lexer::getSpelling(), clang::Lexer::getImmediateMacroName().
> +  StringRef getMacroNameOrSpelling(SourceLocation &Loc);
> +
>  private:
>   ExplodedNode *addTransitionImpl(const ProgramState *State,
>                                  bool MarkAsSink,
>
> Modified: cfe/trunk/lib/StaticAnalyzer/Core/CheckerContext.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/CheckerContext.cpp?rev=148516&r1=148515&r2=148516&view=diff
> ==============================================================================
> --- cfe/trunk/lib/StaticAnalyzer/Core/CheckerContext.cpp (original)
> +++ cfe/trunk/lib/StaticAnalyzer/Core/CheckerContext.cpp Thu Jan 19 18:11:12 2012
> @@ -14,6 +14,7 @@
>
>  #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
>  #include "clang/Basic/Builtins.h"
> +#include "clang/Lex/Lexer.h"
>
>  using namespace clang;
>  using namespace ento;
> @@ -53,3 +54,15 @@
>
>   return false;
>  }
> +
> +StringRef CheckerContext::getMacroNameOrSpelling(SourceLocation &Loc) {
> +  if (!Loc.isMacroID()) {
> +    SmallVector<char, 16> buf;
> +    return Lexer::getSpelling(Loc, buf, getSourceManager(), getLangOptions());
> +  } else {
> +    return Lexer::getImmediateMacroName(Loc, getSourceManager(),
> +                                             getLangOptions());
> +  }
> +  return StringRef();

This last return is trivially unreachable (& found by
-Wunreachable-code). I've refactored this to:

if (Loc.isMacroID())
  return Lexer::getImmediateMacroName...
SmallVector...
return Lexer::getSpelling...

& I'll be checking this in along with a lot of other unreachable-code
fixes in a little while (once I confirm that I don't have any extra
warnings with GCC, etc)

> +}
> +
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits




More information about the cfe-commits mailing list