DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */


#ifndef Utils_h__
#define Utils_h__


#include "CustomAttributes.h"
#include "CustomAttributes.h"
#include "ThirdPartyPaths.h"
#include "ThreadAllows.h"
#include "plugin.h"
#include "plugin.h"

inline StringRef getFilename(const SourceManager &SM, SourceLocation Loc) {
  // We use the presumed location to handle #line directives and such, so the
  // plugin is friendly to icecc / sccache users.
  auto PL = SM.getPresumedLoc(Loc);
  auto PL = SM.getPresumedLoc(Loc);
  if (PL.isValid()) {
    return StringRef(PL.getFilename());
  }
  return SM.getFilename(Loc);
}
}

// Check if the given expression contains an assignment expression.
// This can either take the form of a Binary Operator or a
// Overloaded Operator Call.
inline bool hasSideEffectAssignment(const Expr *Expression) {
inline bool hasSideEffectAssignment(const Expr *Expression) {
  if (auto OpCallExpr = dyn_cast_or_null<CXXOperatorCallExpr>(Expression)) {
    auto BinOp = OpCallExpr->getOperator();
    if (BinOp == OO_Equal || (BinOp >= OO_PlusEqual && BinOp <= OO_PipeEqual)) {
      return true;
    }
    }
  } else if (auto BinOpExpr = dyn_cast_or_null<BinaryOperator>(Expression)) {
    if (BinOpExpr->isAssignmentOp()) {
      return true;
    }
  }


  // Recurse to children.
  for (const Stmt *SubStmt : Expression->children()) {
    auto ChildExpr = dyn_cast_or_null<Expr>(SubStmt);
    if (ChildExpr && hasSideEffectAssignment(ChildExpr)) {
      return true;
      return true;
    }
  }

  return false;
  return false;
}

template <class T>
inline bool ASTIsInSystemHeader(const ASTContext &AC, const T &D) {
  auto &SourceManager = AC.getSourceManager();
  auto &SourceManager = AC.getSourceManager();
  auto ExpansionLoc = SourceManager.getExpansionLoc(D.getBeginLoc());
  if (ExpansionLoc.isInvalid()) {
    return false;
  }
  return SourceManager.isInSystemHeader(ExpansionLoc);
  return SourceManager.isInSystemHeader(ExpansionLoc);
}

template <typename T> inline StringRef getNameChecked(const T &D) {
  return D->getIdentifier() ? D->getName() : "";
}
}

/// A cached data of whether classes are refcounted or not.
typedef DenseMap<const CXXRecordDecl *, std::pair<const Decl *, bool>>
    RefCountedMap;
extern RefCountedMap RefCountedClasses;
extern RefCountedMap RefCountedClasses;

inline bool classHasAddRefRelease(const CXXRecordDecl *D) {
  const RefCountedMap::iterator &It = RefCountedClasses.find(D);
  if (It != RefCountedClasses.end()) {
    return It->second.second;
    return It->second.second;
  }

  bool SeenAddRef = false;
  bool SeenRelease = false;
  bool SeenRelease = false;
  for (CXXRecordDecl::method_iterator Method = D->method_begin();
       Method != D->method_end(); ++Method) {
    const auto &Name = getNameChecked(Method);
    if (Name == "AddRef") {
    if (Name == "AddRef") {
      SeenAddRef = true;
    } else if (Name == "Release") {
      SeenRelease = true;
    }
  }
  }
  RefCountedClasses[D] = std::make_pair(D, SeenAddRef && SeenRelease);
  return SeenAddRef && SeenRelease;
}

inline bool isClassRefCounted(QualType T);
inline bool isClassRefCounted(QualType T);

inline bool isClassRefCounted(const CXXRecordDecl *D) {
  // Normalize so that D points to the definition if it exists.
  if (!D->hasDefinition())
    return false;
    return false;
  D = D->getDefinition();
  // Base class: anyone with AddRef/Release is obviously a refcounted class.
  if (classHasAddRefRelease(D))
    return true;


  // Look through all base cases to figure out if the parent is a refcounted
  // class.
  for (CXXRecordDecl::base_class_const_iterator Base = D->bases_begin();
       Base != D->bases_end(); ++Base) {
    bool Super = isClassRefCounted(Base->getType());
    bool Super = isClassRefCounted(Base->getType());
    if (Super) {
      return true;
    }
  }


  return false;
}

inline bool isClassRefCounted(QualType T) {
  while (const clang::ArrayType *ArrTy = T->getAsArrayTypeUnsafe())
    T = ArrTy->getElementType();
  CXXRecordDecl *Clazz = T->getAsCXXRecordDecl();
  return Clazz ? isClassRefCounted(Clazz) : false;
}
}

inline const FieldDecl *getClassRefCntMember(const CXXRecordDecl *D) {
  for (RecordDecl::field_iterator Field = D->field_begin(), E = D->field_end();
       Field != E; ++Field) {
    if (getNameChecked(Field) == "mRefCnt") {
      return *Field;
    }
  }
  return 0;
  return 0;
}

inline bool typeHasVTable(QualType T) {
  while (const clang::ArrayType *ArrTy = T->getAsArrayTypeUnsafe())
    T = ArrTy->getElementType();
    T = ArrTy->getElementType();
  CXXRecordDecl *Offender = T->getAsCXXRecordDecl();
  return Offender && Offender->hasDefinition() && Offender->isDynamicClass();
}

inline std::string getDeclarationNamespace(const Decl *Declaration) {
inline std::string getDeclarationNamespace(const Decl *Declaration) {
  const DeclContext *DC =
      Declaration->getDeclContext()->getEnclosingNamespaceContext();
  const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
  if (!ND) {
    return "";
    return "";
  }

  while (const DeclContext *ParentDC = ND->getParent()) {
    if (!isa<NamespaceDecl>(ParentDC)) {
      break;
      break;
    }
    ND = cast<NamespaceDecl>(ParentDC);
  }


  const auto &Name = ND->getName();
  return Name;
}


inline bool isInIgnoredNamespaceForImplicitCtor(const Decl *Declaration) {
  std::string Name = getDeclarationNamespace(Declaration);
  if (Name == "") {
    return false;
  }

  return Name == "std" ||               // standard C++ lib
  return Name == "std" ||               // standard C++ lib
         Name == "__gnu_cxx" ||         // gnu C++ lib
         Name == "boost" ||             // boost
         Name == "webrtc" ||            // upstream webrtc
         Name == "rtc" ||               // upstream webrtc 'base' package
         Name.substr(0, 4) == "icu_" || // icu
         Name.substr(0, 4) == "icu_" || // icu
         Name == "google" ||            // protobuf
         Name == "google_breakpad" ||   // breakpad
         Name == "soundtouch" ||        // libsoundtouch
         Name == "stagefright" ||       // libstagefright
         Name == "MacFileUtilities" ||  // MacFileUtilities
         Name == "MacFileUtilities" ||  // MacFileUtilities
         Name == "dwarf2reader" ||      // dwarf2reader
         Name == "arm_ex_to_module" ||  // arm_ex_to_module
         Name == "testing" ||           // gtest
         Name == "Json" ||              // jsoncpp
         Name == "rlbox" ||             // rlbox
         Name == "rlbox" ||             // rlbox
         Name == "v8";                  // irregexp
}

inline bool isInIgnoredNamespaceForImplicitConversion(const Decl *Declaration) {
  std::string Name = getDeclarationNamespace(Declaration);
  std::string Name = getDeclarationNamespace(Declaration);
  if (Name == "") {
    return false;
  }


  return Name == "std" ||             // standard C++ lib
         Name == "__gnu_cxx" ||       // gnu C++ lib
         Name == "google_breakpad" || // breakpad
         Name == "testing" ||         // gtest
         Name == "rlbox";             // rlbox
}


inline bool isIgnoredPathForImplicitConversion(const Decl *Declaration) {
  Declaration = Declaration->getCanonicalDecl();
  SourceLocation Loc = Declaration->getLocation();
  const SourceManager &SM = Declaration->getASTContext().getSourceManager();
  SmallString<1024> FileName = getFilename(SM, Loc);
  SmallString<1024> FileName = getFilename(SM, Loc);
  llvm::sys::fs::make_absolute(FileName);
  llvm::sys::path::reverse_iterator Begin = llvm::sys::path::rbegin(FileName),
                                    End = llvm::sys::path::rend(FileName);
  for (; Begin != End; ++Begin) {
    if (Begin->compare_lower(StringRef("graphite2")) == 0) {
    if (Begin->compare_lower(StringRef("graphite2")) == 0) {
      return true;
    }
    if (Begin->compare_lower(StringRef("chromium")) == 0) {
      // Ignore security/sandbox/chromium but not ipc/chromium.
      ++Begin;
      ++Begin;
      return Begin != End && Begin->compare_lower(StringRef("sandbox")) == 0;
    }
  }
  return false;
}
}

inline bool isIgnoredPathForSprintfLiteral(const CallExpr *Call,
                                           const SourceManager &SM) {
  SourceLocation Loc = Call->getBeginLoc();
  SmallString<1024> FileName = getFilename(SM, Loc);
  SmallString<1024> FileName = getFilename(SM, Loc);
  llvm::sys::fs::make_absolute(FileName);
  llvm::sys::path::reverse_iterator Begin = llvm::sys::path::rbegin(FileName),
                                    End = llvm::sys::path::rend(FileName);
  for (; Begin != End; ++Begin) {
  for (; Begin != End; ++Begin) {
    if (Begin->compare_lower(StringRef("angle")) == 0 ||
        Begin->compare_lower(StringRef("chromium")) == 0 ||
        Begin->compare_lower(StringRef("crashreporter")) == 0 ||
        Begin->compare_lower(StringRef("google-breakpad")) == 0 ||
        Begin->compare_lower(StringRef("gflags")) == 0 ||
        Begin->compare_lower(StringRef("gflags")) == 0 ||
        Begin->compare_lower(StringRef("harfbuzz")) == 0 ||
        Begin->compare_lower(StringRef("icu")) == 0 ||
        Begin->compare_lower(StringRef("jsoncpp")) == 0 ||
        Begin->compare_lower(StringRef("libstagefright")) == 0 ||
        Begin->compare_lower(StringRef("mtransport")) == 0 ||
        Begin->compare_lower(StringRef("mtransport")) == 0 ||
        Begin->compare_lower(StringRef("protobuf")) == 0 ||
        Begin->compare_lower(StringRef("skia")) == 0 ||
        Begin->compare_lower(StringRef("sfntly")) == 0 ||
        // Gtest uses snprintf as GTEST_SNPRINTF_ with sizeof
        Begin->compare_lower(StringRef("testing")) == 0) {
      return true;
    }
    }
    if (Begin->compare_lower(StringRef("webrtc")) == 0) {
      // Ignore trunk/webrtc, but not media/webrtc
      ++Begin;
      return Begin != End && Begin->compare_lower(StringRef("trunk")) == 0;
    }
    }
  }
  return false;
}

inline bool isInterestingDeclForImplicitConversion(const Decl *Declaration) {
inline bool isInterestingDeclForImplicitConversion(const Decl *Declaration) {
  return !isInIgnoredNamespaceForImplicitConversion(Declaration) &&
  return !isInIgnoredNamespaceForImplicitConversion(Declaration) &&
         !isIgnoredPathForImplicitConversion(Declaration);
}

inline bool isIgnoredExprForMustUse(const Expr *E) {
  if (const CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(E)) {
  if (const CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(E)) {
    switch (OpCall->getOperator()) {
    case OO_Equal:
    case OO_PlusEqual:
    case OO_MinusEqual:
    case OO_StarEqual:
    case OO_StarEqual:
    case OO_SlashEqual:
    case OO_PercentEqual:
    case OO_CaretEqual:
    case OO_AmpEqual:
    case OO_PipeEqual:
    case OO_PipeEqual:
    case OO_LessLessEqual:
    case OO_GreaterGreaterEqual:
      return true;
    default:
      return false;
    }
  }
  }

  if (const BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) {
    return Op->isAssignmentOp();
  }


  return false;
}

inline bool typeIsRefPtr(QualType Q) {
  CXXRecordDecl *D = Q->getAsCXXRecordDecl();
  CXXRecordDecl *D = Q->getAsCXXRecordDecl();
  if (!D || !D->getIdentifier()) {
    return false;
  }

  StringRef name = D->getName();
  StringRef name = D->getName();
  if (name == "RefPtr" || name == "nsCOMPtr") {
    return true;
  }
  return false;
}

// The method defined in clang for ignoring implicit nodes doesn't work with
// some AST trees. To get around this, we define our own implementation of
// some AST trees. To get around this, we define our own implementation of
// IgnoreTrivials.
inline const Stmt *MaybeSkipOneTrivial(const Stmt *s) {
  if (!s) {
    return nullptr;
  }
  if (auto *ewc = dyn_cast<ExprWithCleanups>(s)) {
    return ewc->getSubExpr();
    return ewc->getSubExpr();
  }
  if (auto *mte = dyn_cast<MaterializeTemporaryExpr>(s)) {
    // With clang 10 and up `getTemporary` has been replaced with the more
    // versatile `getSubExpr`.
#if CLANG_VERSION_FULL >= 1000
#if CLANG_VERSION_FULL >= 1000
    return mte->getSubExpr();
#else
    return mte->GetTemporaryExpr();
#endif
  }
  }
  if (auto *bte = dyn_cast<CXXBindTemporaryExpr>(s)) {
    return bte->getSubExpr();
  }
  if (auto *ce = dyn_cast<CastExpr>(s)) {
    s = ce->getSubExpr();
    s = ce->getSubExpr();
  }
  if (auto *pe = dyn_cast<ParenExpr>(s)) {
    s = pe->getSubExpr();
  }
  // Not a trivial.
  // Not a trivial.
  return s;
}

inline const Stmt *IgnoreTrivials(const Stmt *s) {
inline const Stmt *IgnoreTrivials(const Stmt *s) {
  while (true) {
    const Stmt *newS = MaybeSkipOneTrivial(s);
    if (newS == s) {
      return newS;
    }
    s = newS;
    s = newS;
  }

  // Unreachable
  return nullptr;
}
}

inline const Expr *IgnoreTrivials(const Expr *e) {
  return cast_or_null<Expr>(IgnoreTrivials(static_cast<const Stmt *>(e)));
}


// Returns the input if the input is not a trivial.
inline const Expr *MaybeSkipOneTrivial(const Expr *e) {
  return cast_or_null<Expr>(MaybeSkipOneTrivial(static_cast<const Stmt *>(e)));
}


const FieldDecl *getBaseRefCntMember(QualType T);

inline const FieldDecl *getBaseRefCntMember(const CXXRecordDecl *D) {
  const FieldDecl *RefCntMember = getClassRefCntMember(D);
  if (RefCntMember && isClassRefCounted(D)) {
  if (RefCntMember && isClassRefCounted(D)) {
    return RefCntMember;
  }

  for (CXXRecordDecl::base_class_const_iterator Base = D->bases_begin(),
  for (CXXRecordDecl::base_class_const_iterator Base = D->bases_begin(),
                                                E = D->bases_end();
       Base != E; ++Base) {
    RefCntMember = getBaseRefCntMember(Base->getType());
    if (RefCntMember) {
      return RefCntMember;
      return RefCntMember;
    }
  }
  return 0;
}


inline const FieldDecl *getBaseRefCntMember(QualType T) {
  while (const clang::ArrayType *ArrTy = T->getAsArrayTypeUnsafe())
    T = ArrTy->getElementType();
  CXXRecordDecl *Clazz = T->getAsCXXRecordDecl();
  return Clazz ? getBaseRefCntMember(Clazz) : 0;
}


inline bool isPlacementNew(const CXXNewExpr *Expression) {
  // Regular new expressions aren't placement new
  if (Expression->getNumPlacementArgs() == 0)
    return false;
  const FunctionDecl *Declaration = Expression->getOperatorNew();
  const FunctionDecl *Declaration = Expression->getOperatorNew();
  if (Declaration && hasCustomAttribute<moz_heap_allocator>(Declaration)) {
    return false;
  }
  return true;
}
}

extern DenseMap<StringRef, bool> InThirdPartyPathCache;

inline bool inThirdPartyPath(SourceLocation Loc, const SourceManager &SM) {
  StringRef OriginalFileName = getFilename(SM, Loc);
  StringRef OriginalFileName = getFilename(SM, Loc);
  auto pair = InThirdPartyPathCache.find(OriginalFileName);
  if (pair != InThirdPartyPathCache.end()) {
    return pair->second;
  }
  }

  SmallString<1024> FileName = OriginalFileName;
  llvm::sys::fs::make_absolute(FileName);

  for (uint32_t i = 0; i < MOZ_THIRD_PARTY_PATHS_COUNT; ++i) {
  for (uint32_t i = 0; i < MOZ_THIRD_PARTY_PATHS_COUNT; ++i) {
    auto PathB = sys::path::begin(FileName);
    auto PathE = sys::path::end(FileName);

    auto ThirdPartyB = sys::path::begin(MOZ_THIRD_PARTY_PATHS[i]);
    auto ThirdPartyE = sys::path::end(MOZ_THIRD_PARTY_PATHS[i]);
    auto ThirdPartyE = sys::path::end(MOZ_THIRD_PARTY_PATHS[i]);

    for (; PathB != PathE; ++PathB) {
      // Perform an inner loop to compare path segments, checking if the current
      // segment is the start of the current third party path.
      auto IPathB = PathB;
      auto IPathB = PathB;
      auto IThirdPartyB = ThirdPartyB;
      for (; IPathB != PathE && IThirdPartyB != ThirdPartyE;
           ++IPathB, ++IThirdPartyB) {
        if (IPathB->compare_lower(*IThirdPartyB) != 0) {
          break;
        }
      }


      // We found a match!
      if (IThirdPartyB == ThirdPartyE) {
        InThirdPartyPathCache.insert(std::make_pair(OriginalFileName, true));
        return true;
      }
      }
    }
  }

  InThirdPartyPathCache.insert(std::make_pair(OriginalFileName, false));
  return false;
  return false;
}

inline bool inThirdPartyPath(const Decl *D, ASTContext *context) {
  D = D->getCanonicalDecl();
  SourceLocation Loc = D->getLocation();
  SourceLocation Loc = D->getLocation();
  const SourceManager &SM = context->getSourceManager();

  return inThirdPartyPath(Loc, SM);
}


inline CXXRecordDecl *getNonTemplateSpecializedCXXRecordDecl(QualType Q) {
  auto *D = Q->getAsCXXRecordDecl();

  if (!D) {
    auto TemplateQ = Q->getAs<TemplateSpecializationType>();
    if (!TemplateQ) {
      return nullptr;
    }


    auto TemplateDecl = TemplateQ->getTemplateName().getAsTemplateDecl();
    if (!TemplateDecl) {
      return nullptr;
    }


    D = dyn_cast_or_null<CXXRecordDecl>(TemplateDecl->getTemplatedDecl());
    if (!D) {
      return nullptr;
    }
  }
  }

  return D;
}

inline bool inThirdPartyPath(const Decl *D) {
inline bool inThirdPartyPath(const Decl *D) {
  return inThirdPartyPath(D, &D->getASTContext());
}

inline bool inThirdPartyPath(const Stmt *S, ASTContext *context) {
  SourceLocation Loc = S->getBeginLoc();
  SourceLocation Loc = S->getBeginLoc();
  const SourceManager &SM = context->getSourceManager();
  auto ExpansionLoc = SM.getExpansionLoc(Loc);
  if (ExpansionLoc.isInvalid()) {
    return inThirdPartyPath(Loc, SM);
  }
  }
  return inThirdPartyPath(ExpansionLoc, SM);
}

/// Polyfill for CXXOperatorCallExpr::isInfixBinaryOp()
inline bool isInfixBinaryOp(const CXXOperatorCallExpr *OpCall) {
inline bool isInfixBinaryOp(const CXXOperatorCallExpr *OpCall) {
#if CLANG_VERSION_FULL >= 400
  return OpCall->isInfixBinaryOp();
#else
#else
  // Taken from clang source.
  // Taken from clang source.
  if (OpCall->getNumArgs() != 2)
    return false;


  switch (OpCall->getOperator()) {
  case OO_Call:
  case OO_Subscript:
    return false;
  default:
  default:
    return true;
  }
#endif
}


#endif