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.

Header

Mercurial (409f3966645a)

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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#include "DisplayListChecker.h"

#include "gfxPrefs.h"
#include "nsDisplayList.h"

namespace mozilla {

class DisplayItemBlueprint;

// Stack node used during tree visits, to store the path to a display item.
struct DisplayItemBlueprintStack
{
  const DisplayItemBlueprintStack* mPrevious;
  const DisplayItemBlueprint* mItem;
  // Output stack to aSs, with format "name#index > ... > name#index".
  // Returns true if anything was output, false if empty.
  bool Output(std::stringstream& aSs) const;
};

// Object representing a list of display items (either the top of the tree, or
// an item's children), with just enough information to compare with another
// tree and output useful debugging information.
class DisplayListBlueprint
{
public:
  DisplayListBlueprint(nsDisplayList* aList, const char* aName)
    : DisplayListBlueprint(aList, 0, aName)
  {
  }

  DisplayListBlueprint(nsDisplayList* aList,
                       const char* aName,
                       unsigned& aIndex)
  {
    processChildren(aList, aName, aIndex);
  }

  // Find a display item with the given frame and per-frame key.
  // Returns empty string if not found.
  std::string Find(const nsIFrame* aFrame, uint32_t aPerFrameKey) const
  {
    const DisplayItemBlueprintStack stack{ nullptr, nullptr };
    return Find(aFrame, aPerFrameKey, stack);
  }

  std::string Find(const nsIFrame* aFrame,
                   uint32_t aPerFrameKey,
                   const DisplayItemBlueprintStack& aStack) const;

  // Compare this list with another one, output differences between the two
  // into aDiff.
  // Differences include: Display items from one tree for which a corresponding
  // item (same frame and per-frame key) cannot be found under corresponding
  // parent items.
  // Returns true if trees are similar, false if different.
  bool CompareList(const DisplayListBlueprint& aOther,
                   std::stringstream& aDiff) const
  {
    const DisplayItemBlueprintStack stack{ nullptr, nullptr };
    const bool ab = CompareList(*this, aOther, aOther, aDiff, stack, stack);
    const bool ba =
      aOther.CompareList(aOther, *this, *this, aDiff, stack, stack);
    return ab && ba;
  }

  bool CompareList(const DisplayListBlueprint& aRoot,
                   const DisplayListBlueprint& aOther,
                   const DisplayListBlueprint& aOtherRoot,
                   std::stringstream& aDiff,
                   const DisplayItemBlueprintStack& aStack,
                   const DisplayItemBlueprintStack& aStackOther) const;

  // Output this tree to aSs.
  void Dump(std::stringstream& aSs) const { Dump(aSs, 0); }

  void Dump(std::stringstream& aSs, unsigned aDepth) const;

private:
  // Only used by first constructor, to call the 2nd constructor with an index
  // variable on the stack.
  DisplayListBlueprint(nsDisplayList* aList, unsigned aIndex, const char* aName)
    : DisplayListBlueprint(aList, aName, aIndex)
  {
  }

  void processChildren(nsDisplayList* aList,
                       const char* aName,
                       unsigned& aIndex);

  std::vector<DisplayItemBlueprint> mItems;
  const bool mVerifyOrder = gfxPrefs::LayoutVerifyRetainDisplayListOrder();
};

// Object representing one display item, with just enough information to
// compare with another item and output useful debugging information.
class DisplayItemBlueprint
{
public:
  DisplayItemBlueprint(nsDisplayItem& aItem,
                       const char* aName,
                       unsigned& aIndex)
    : mListName(aName)
    , mIndex(++aIndex)
    , mIndexString(WriteIndex(aName, aIndex))
    , mIndexStringFW(WriteIndexFW(aName, aIndex))
    , mDisplayItemPointer(WriteDisplayItemPointer(aItem))
    , mDescription(WriteDescription(aName, aIndex, aItem))
    , mFrame(aItem.HasDeletedFrame() ? nullptr : aItem.Frame())
    , mPerFrameKey(aItem.GetPerFrameKey())
    , mChildren(aItem.GetChildren(), aName, aIndex)
  {
  }

  // Compare this item with another one, based on frame and per-frame key.
  // Not recursive! I.e., children are not examined.
  bool CompareItem(const DisplayItemBlueprint& aOther,
                   std::stringstream& aDiff) const
  {
    return mFrame == aOther.mFrame && mPerFrameKey == aOther.mPerFrameKey;
  }

  void Dump(std::stringstream& aSs, unsigned aDepth) const;

  const char* mListName;
  const unsigned mIndex;
  const std::string mIndexString;
  const std::string mIndexStringFW;
  const std::string mDisplayItemPointer;
  const std::string mDescription;

  // For pointer comparison only, do not dereference!
  const nsIFrame* const mFrame;
  const uint32_t mPerFrameKey;

  const DisplayListBlueprint mChildren;

private:
  static std::string WriteIndex(const char* aName, unsigned aIndex)
  {
    return nsPrintfCString("%s#%u", aName, aIndex).get();
  }

  static std::string WriteIndexFW(const char* aName, unsigned aIndex)
  {
    return nsPrintfCString("%s#%4u", aName, aIndex).get();
  }

  static std::string WriteDisplayItemPointer(nsDisplayItem& aItem)
  {
    return nsPrintfCString("0x%p", &aItem).get();
  }

  static std::string WriteDescription(const char* aName,
                                      unsigned aIndex,
                                      nsDisplayItem& aItem)
  {
    if (aItem.HasDeletedFrame()) {
      return nsPrintfCString(
               "%s %s#%u 0x%p f=0x0", aItem.Name(), aName, aIndex, &aItem)
        .get();
    }

    const nsIFrame* f = aItem.Frame();
    nsAutoString contentData;
#ifdef DEBUG_FRAME_DUMP
    f->GetFrameName(contentData);
#endif
    nsIContent* content = f->GetContent();
    if (content) {
      nsString tmp;
      if (content->GetID()) {
        content->GetID()->ToString(tmp);
        contentData.AppendLiteral(" id:");
        contentData.Append(tmp);
      }
      const nsAttrValue* classes =
        content->IsElement() ? content->AsElement()->GetClasses() : nullptr;
      if (classes) {
        classes->ToString(tmp);
        contentData.AppendLiteral(" class:");
        contentData.Append(tmp);
      }
    }
    return nsPrintfCString("%s %s#%u p=0x%p f=0x%p(%s) key=%" PRIu32,
                           aItem.Name(),
                           aName,
                           aIndex,
                           &aItem,
                           f,
                           NS_ConvertUTF16toUTF8(contentData).get(),
                           aItem.GetPerFrameKey())
      .get();
  }
};

void
DisplayListBlueprint::processChildren(nsDisplayList* aList,
                                      const char* aName,
                                      unsigned& aIndex)
{
  if (!aList) {
    return;
  }
  const uint32_t n = aList->Count();
  if (n == 0) {
    return;
  }
  mItems.reserve(n);
  for (nsDisplayItem* item = aList->GetBottom(); item;
       item = item->GetAbove()) {
    mItems.emplace_back(*item, aName, aIndex);
  }
  MOZ_ASSERT(mItems.size() == n);
}

bool
DisplayItemBlueprintStack::Output(std::stringstream& aSs) const
{
  const bool output = mPrevious ? mPrevious->Output(aSs) : false;
  if (mItem) {
    if (output) {
      aSs << " > ";
    }
    aSs << mItem->mIndexString;
    return true;
  }
  return output;
}

std::string
DisplayListBlueprint::Find(const nsIFrame* aFrame,
                           uint32_t aPerFrameKey,
                           const DisplayItemBlueprintStack& aStack) const
{
  for (const DisplayItemBlueprint& item : mItems) {
    if (item.mFrame == aFrame && item.mPerFrameKey == aPerFrameKey) {
      std::stringstream ss;
      if (aStack.Output(ss)) {
        ss << " > ";
      }
      ss << item.mDescription;
      return ss.str();
    }
    const DisplayItemBlueprintStack stack = { &aStack, &item };
    std::string s = item.mChildren.Find(aFrame, aPerFrameKey, stack);
    if (!s.empty()) {
      return s;
    }
  }
  return "";
}

bool
DisplayListBlueprint::CompareList(
  const DisplayListBlueprint& aRoot,
  const DisplayListBlueprint& aOther,
  const DisplayListBlueprint& aOtherRoot,
  std::stringstream& aDiff,
  const DisplayItemBlueprintStack& aStack,
  const DisplayItemBlueprintStack& aStackOther) const
{
  bool same = true;
  unsigned previousFoundIndex = 0;
  const DisplayItemBlueprint* previousFoundItemBefore = nullptr;
  const DisplayItemBlueprint* previousFoundItemAfter = nullptr;
  for (const DisplayItemBlueprint& itemBefore : mItems) {
    bool found = false;
    unsigned foundIndex = 0;
    for (const DisplayItemBlueprint& itemAfter : aOther.mItems) {
      if (itemBefore.CompareItem(itemAfter, aDiff)) {
        found = true;

        if (mVerifyOrder) {
          if (foundIndex < previousFoundIndex) {
            same = false;
            aDiff << "\n";
            if (aStack.Output(aDiff)) {
              aDiff << " > ";
            }
            aDiff << itemBefore.mDescription;
            aDiff << "\n * Corresponding item in unexpected order: ";
            if (aStackOther.Output(aDiff)) {
              aDiff << " > ";
            }
            aDiff << itemAfter.mDescription;
            aDiff << "\n * Was expected after: ";
            if (aStackOther.Output(aDiff)) {
              aDiff << " > ";
            }
            MOZ_ASSERT(previousFoundItemAfter);
            aDiff << previousFoundItemAfter->mDescription;
            aDiff << "\n   which corresponds to: ";
            if (aStack.Output(aDiff)) {
              aDiff << " > ";
            }
            MOZ_ASSERT(previousFoundItemBefore);
            aDiff << previousFoundItemBefore->mDescription;
          }
          previousFoundIndex = foundIndex;
          previousFoundItemBefore = &itemBefore;
          previousFoundItemAfter = &itemAfter;
        }

        const DisplayItemBlueprintStack stack = { &aStack, &itemBefore };
        const DisplayItemBlueprintStack stackOther = { &aStackOther,
                                                       &itemAfter };
        if (!itemBefore.mChildren.CompareList(aRoot,
                                              itemAfter.mChildren,
                                              aOtherRoot,
                                              aDiff,
                                              stack,
                                              stackOther)) {
          same = false;
        }
        break;
      }
      ++foundIndex;
    }
    if (!found) {
      same = false;
      aDiff << "\n";
      if (aStack.Output(aDiff)) {
        aDiff << " > ";
      }
      aDiff << itemBefore.mDescription;
      aDiff << "\n * Cannot find corresponding item under ";
      if (!aStackOther.Output(aDiff)) {
        if (!aOtherRoot.mItems.empty()) {
          aDiff << aOtherRoot.mItems[0].mListName;
        } else {
          aDiff << "other root";
        }
      }
      std::string elsewhere =
        aOtherRoot.Find(itemBefore.mFrame, itemBefore.mPerFrameKey);
      if (!elsewhere.empty()) {
        aDiff << "\n * But found: " << elsewhere;
      }
    }
  }
  return same;
}

void
DisplayListBlueprint::Dump(std::stringstream& aSs, unsigned aDepth) const
{
  for (const DisplayItemBlueprint& item : mItems) {
    item.Dump(aSs, aDepth);
  }
}

void
DisplayItemBlueprint::Dump(std::stringstream& aSs, unsigned aDepth) const
{
  aSs << "\n" << mIndexStringFW << " ";
  for (unsigned i = 0; i < aDepth; ++i) {
    aSs << "  ";
  }
  aSs << mDescription;
  mChildren.Dump(aSs, aDepth + 1);
}

DisplayListChecker::DisplayListChecker()
  : mBlueprint(nullptr)
{
}

DisplayListChecker::DisplayListChecker(nsDisplayList* aList, const char* aName)
  : mBlueprint(MakeUnique<DisplayListBlueprint>(aList, aName))
{
}

DisplayListChecker::~DisplayListChecker() = default;

void
DisplayListChecker::Set(nsDisplayList* aList, const char* aName)
{
  mBlueprint = MakeUnique<DisplayListBlueprint>(aList, aName);
}

// Compare this list with another one, output differences between the two
// into aDiff.
// Differences include: Display items from one tree for which a corresponding
// item (same frame and per-frame key) cannot be found under corresponding
// parent items.
// Returns true if trees are similar, false if different.
bool
DisplayListChecker::CompareList(const DisplayListChecker& aOther,
                                std::stringstream& aDiff) const
{
  MOZ_ASSERT(mBlueprint);
  MOZ_ASSERT(aOther.mBlueprint);
  return mBlueprint->CompareList(*aOther.mBlueprint, aDiff);
}

void
DisplayListChecker::Dump(std::stringstream& aSs) const
{
  MOZ_ASSERT(mBlueprint);
  mBlueprint->Dump(aSs);
}

} // namespace mozilla