skia2/tools/sk_app/CommandSet.cpp
Kevin Lubick e253cc3e55 [bazel] Use toolchain features to opt files into being IWYU compliant.
PS1 regenerates the Bazel files.

It is recommended to review this CL with a diff from PS1.

Example output when a file does not pass the test:
    tools/sk_app/CommandSet.h should add these lines:
    #include "include/core/SkTypes.h"
    #include "include/private/SkTArray.h"
    #include "tools/skui/InputState.h"
    #include "tools/skui/Key.h"
    #include "tools/skui/ModifierKey.h"
    namespace sk_app { class Window; }

    tools/sk_app/CommandSet.h should remove these lines:
    - #include "tools/sk_app/Window.h"

    The full include-list for tools/sk_app/CommandSet.h:
    #include "include/core/SkString.h"
    #include "include/core/SkTypes.h"
    #include "include/private/SkTArray.h"
    #include "tools/skui/InputState.h"
    #include "tools/skui/Key.h"
    #include "tools/skui/ModifierKey.h"
    #include <functional>
    #include <vector>
    class SkCanvas;
    namespace sk_app { class Window; }
    ---

This makes use of Bazel's toolchain features
https://bazel.build/docs/cc-toolchain-config-reference#features
to allow us to configure compiler flags when compiling
individual files. This analysis is off by default, and can
be turned on with --features skia_enforce_iwyu. When enabled,
it will only be run for files that have opted in.
Example:
    bazelisk build //example:hello_world_gl --config=clang \
       --sandbox_base=/dev/shm --features skia_enforce_iwyu

There are two ways to opt files in:
 - Add enforce_iwyu = True to a generated_cc_atom rule
 - Add enforce_iwyu_on_package() to a BUILD.bazel file
   (which enforces IWYU for all rules in that file)

Note that Bazel does not propagate features to dependencies
or dependents, so trying to enable the feature on cc_library
or cc_executable targets will only impact any files listed in
srcs or hdrs, not deps. This may be counter-intuitive when
compared to things like defines.

IWYU supports a mapping file, which we supply to help properly
handle things system headers (//toolchain/IWYU_mapping.imp)

Suggested Review Order:
 - toolchain/build_toolchain.bzl to see how we get the IWYU
   binaries into the toolchain
 - toolchain/BUILD.bazel and toolchain/IWYU_mapping.imp
   to see how the mapping file is made available for
   all compile steps
 - toolchain/clang_toolchain_config.bzl, where we define the
   skia_enforce_iwyu feature to turn on any verification at
   all and skia_opt_file_into_iwyu to enable the check for
   specific files using a define.
 - toolchain/clang_trampoline.sh, which is the toolchain is
   configured to call instead of clang directly (see line 83
   of clang_toolchain_config.bzl). This bash script used to
   just forward all arguments directly onto clang. Now it
   inspects them and either calls clang directly (if
   it does not find the define in the arguments or we are
   linking [bazel sometimes links with clang instead of ld])
   or calls clang and then include-what-you-use. In all cases,
   the trampoline sends the arguments to clang and IWYU
   unchanged).
 - //tools/sk_app/... to see enforcement enabled (and fixed)
   for select files, as an example of that method.
 - //experimental/bazel_test/... to see enforcement enabled
   for all rules in a BUILD.bazel file.
 - all other files.

Change-Id: I60a2ea9d5dc9955b6a8f166bd449de9e2b81a233
Bug: skia:13052
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/519776
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Leandro Lovisolo <lovisolo@google.com>
Commit-Queue: Kevin Lubick <kjlubick@google.com>
2022-03-16 13:09:46 +00:00

170 lines
5.1 KiB
C++

/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "tools/sk_app/CommandSet.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontTypes.h"
#include "include/core/SkPaint.h"
#include "include/core/SkScalar.h"
#include "src/core/SkStringUtils.h"
#include "tools/sk_app/Window.h"
#include <algorithm>
namespace sk_app {
CommandSet::CommandSet()
: fHelpMode(kNone_HelpMode) {
this->addCommand('h', "Overlays", "Show help screen", [this]() {
switch (this->fHelpMode) {
case kNone_HelpMode:
this->fHelpMode = kGrouped_HelpMode;
break;
case kGrouped_HelpMode:
this->fHelpMode = kAlphabetical_HelpMode;
break;
case kAlphabetical_HelpMode:
this->fHelpMode = kNone_HelpMode;
break;
}
fWindow->inval();
});
}
void CommandSet::attach(Window* window) {
fWindow = window;
}
bool CommandSet::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
if (skui::InputState::kDown == state) {
for (Command& cmd : fCommands) {
if (Command::kKey_CommandType == cmd.fType && key == cmd.fKey) {
cmd.fFunction();
return true;
}
}
}
return false;
}
bool CommandSet::onChar(SkUnichar c, skui::ModifierKey modifiers) {
for (Command& cmd : fCommands) {
if (Command::kChar_CommandType == cmd.fType && c == cmd.fChar) {
cmd.fFunction();
return true;
}
}
return false;
}
bool CommandSet::onSoftkey(const SkString& softkey) {
for (const Command& cmd : fCommands) {
if (cmd.getSoftkeyString().equals(softkey)) {
cmd.fFunction();
return true;
}
}
return false;
}
void CommandSet::addCommand(SkUnichar c, const char* group, const char* description,
std::function<void(void)> function) {
fCommands.push_back(Command(c, group, description, function));
}
void CommandSet::addCommand(skui::Key k, const char* keyName, const char* group,
const char* description, std::function<void(void)> function) {
fCommands.push_back(Command(k, keyName, group, description, function));
}
bool CommandSet::compareCommandKey(const Command& first, const Command& second) {
return SK_strcasecmp(first.fKeyName.c_str(), second.fKeyName.c_str()) < 0;
}
bool CommandSet::compareCommandGroup(const Command& first, const Command& second) {
return SK_strcasecmp(first.fGroup.c_str(), second.fGroup.c_str()) < 0;
}
void CommandSet::drawHelp(SkCanvas* canvas) {
if (kNone_HelpMode == fHelpMode) {
return;
}
// Sort commands for current mode:
std::stable_sort(fCommands.begin(), fCommands.end(),
kAlphabetical_HelpMode == fHelpMode ? compareCommandKey : compareCommandGroup);
SkFont font;
font.setSize(16);
SkFont groupFont;
groupFont.setSize(18);
SkPaint bgPaint;
bgPaint.setColor(0xC0000000);
canvas->drawPaint(bgPaint);
SkPaint paint;
paint.setAntiAlias(true);
paint.setColor(0xFFFFFFFF);
SkPaint groupPaint;
groupPaint.setAntiAlias(true);
groupPaint.setColor(0xFFFFFFFF);
SkScalar x = SkIntToScalar(10);
SkScalar y = SkIntToScalar(10);
// Measure all key strings:
SkScalar keyWidth = 0;
for (Command& cmd : fCommands) {
keyWidth = std::max(keyWidth,
font.measureText(cmd.fKeyName.c_str(), cmd.fKeyName.size(),
SkTextEncoding::kUTF8));
}
keyWidth += font.measureText(" ", 1, SkTextEncoding::kUTF8);
// If we're grouping by category, we'll be adding text height on every new group (including the
// first), so no need to do that here. Otherwise, skip down so the first line is where we want.
if (kGrouped_HelpMode != fHelpMode) {
y += font.getSize();
}
// Print everything:
SkString lastGroup;
for (Command& cmd : fCommands) {
if (kGrouped_HelpMode == fHelpMode && lastGroup != cmd.fGroup) {
// Group change. Advance and print header:
y += font.getSize();
canvas->drawSimpleText(cmd.fGroup.c_str(), cmd.fGroup.size(), SkTextEncoding::kUTF8,
x, y, groupFont, groupPaint);
y += groupFont.getSize() + 2;
lastGroup = cmd.fGroup;
}
canvas->drawSimpleText(cmd.fKeyName.c_str(), cmd.fKeyName.size(), SkTextEncoding::kUTF8,
x, y, font, paint);
SkString text = SkStringPrintf(": %s", cmd.fDescription.c_str());
canvas->drawString(text, x + keyWidth, y, font, paint);
y += font.getSize() + 2;
}
}
std::vector<SkString> CommandSet::getCommandsAsSoftkeys() const {
std::vector<SkString> result;
for(const Command& command : fCommands) {
result.push_back(command.getSoftkeyString());
}
return result;
}
} // namespace sk_app