Skip to content

Commit

Permalink
Merge pull request #83 from xatier/master
Browse files Browse the repository at this point in the history
chore: fix issues raised by clang-tidy
  • Loading branch information
lukhnos authored Aug 17, 2023
2 parents f9bb1b5 + f93f709 commit 6de44eb
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if (ENABLE_CLANG_TIDY)
MESSAGE(STATUS "Enabled clang-tidy")
find_program(CLANG_TIDY NAMES "clang-tidy" REQUIRED)
MESSAGE(STATUS "Found clang-tidy: ${CLANG_TIDY}")
set(CLANG_TIDY_COMMAND "${CLANG_TIDY}" "-checks=-*,clang-analyzer-*,bugprone-*,cert-*,readability-*")
set(CLANG_TIDY_COMMAND "${CLANG_TIDY}" "-checks=-*,clang-analyzer-*,bugprone-*,-bugprone-easily-swappable-parameters,cert-*,readability-*")
# one may wants to use the following instead to focus on a specific category
# set(CLANG_TIDY_COMMAND "${CLANG_TIDY}" "-checks=-*,clang-analyzer-*")
endif ()
Expand Down
19 changes: 14 additions & 5 deletions src/Engine/Mandarin/Mandarin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ const BPMF BPMF::FromHanyuPinyin(const std::string& str) {
// lookup consonants and consume them
bool independentConsonant = false;

// disable this check for if-else/switch blocks below
// NOLINTBEGIN(bugprone-branch-clone)

// the y exceptions fist
if (0) {
} else if (PinyinParseHelper::ConsumePrefix(pinyin, "yuan")) {
Expand Down Expand Up @@ -101,7 +104,7 @@ const BPMF BPMF::FromHanyuPinyin(const std::string& str) {
}

// try the first character
char c = pinyin.length() ? pinyin[0] : 0;
char c = pinyin.length() ? pinyin[0] : '\0';
switch (c) {
case 'b':
firstComponent = BPMF::B;
Expand Down Expand Up @@ -160,7 +163,7 @@ const BPMF BPMF::FromHanyuPinyin(const std::string& str) {
pinyin = pinyin.substr(1);
break;

// special hanlding for w and y
// special handling for w and y
case 'w':
secondComponent = BPMF::U;
pinyin = pinyin.substr(1);
Expand Down Expand Up @@ -316,6 +319,7 @@ const BPMF BPMF::FromHanyuPinyin(const std::string& str) {
thirdComponent = BPMF::ER;
}
}
// NOLINTEND(bugprone-branch-clone)

// at last!
if (0) {
Expand Down Expand Up @@ -447,6 +451,7 @@ const std::string BPMF::HanyuPinyinString(bool includesTone,
break;
}

// NOLINTBEGIN(bugprone-branch-clone)
switch (vc) {
case A:
vowel = "a";
Expand Down Expand Up @@ -488,6 +493,7 @@ const std::string BPMF::HanyuPinyinString(bool includesTone,
vowel = "er";
break;
}
// NOLINTEND(bugprone-branch-clone)

// combination rules

Expand Down Expand Up @@ -566,7 +572,7 @@ const BPMF BPMF::FromComposedString(const std::string& str) {
break;
}

size_t utf8_length = -1;
std::string::difference_type utf8_length = -1;

// These are the code points for the tone markers.
if ((*iter & (0x80 | 0x40)) && !(*iter & 0x20)) {
Expand Down Expand Up @@ -600,10 +606,10 @@ const BPMF BPMF::FromComposedString(const std::string& str) {
const std::string BPMF::composedString() const {
std::string result;
#define APPEND(c) \
if (syllable_ & c) \
if (syllable_ & (c)) \
result += \
(*BopomofoCharacterMap::SharedInstance().componentToCharacter.find( \
syllable_ & c)) \
syllable_ & (c))) \
.second
APPEND(ConsonantMask);
APPEND(MiddleVowelMask);
Expand Down Expand Up @@ -667,6 +673,8 @@ BopomofoCharacterMap::BopomofoCharacterMap() {
componentToCharacter[(*iter).second] = (*iter).first;
}

// we don't need parentheses for these macros
// NOLINTBEGIN(bugprone-macro-parentheses)
#define ASSIGNKEY1(m, vec, k, val) \
m[k] = (vec.clear(), vec.push_back((BPMF::Component)val), vec)
#define ASSIGNKEY2(m, vec, k, val1, val2) \
Expand All @@ -676,6 +684,7 @@ BopomofoCharacterMap::BopomofoCharacterMap() {
m[k] = (vec.clear(), vec.push_back((BPMF::Component)val1), \
vec.push_back((BPMF::Component)val2), \
vec.push_back((BPMF::Component)val3), vec)
// NOLINTEND(bugprone-macro-parentheses)

static BopomofoKeyboardLayout* CreateStandardLayout() {
std::vector<BPMF::Component> vec;
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/ParselessLM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ bool McBopomofo::ParselessLM::open(const std::string_view& path)
}

db_ = std::unique_ptr<ParselessPhraseDB>(new ParselessPhraseDB(
static_cast<char*>(data_), length_, /*validate_pragme=*/
static_cast<char*>(data_), length_, /*validate_pragma=*/
true));
return true;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Engine/PhraseReplacementMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ bool PhraseReplacementMap::open(const char* path)

KeyValueBlobReader reader(static_cast<char*>(data), length);
KeyValueBlobReader::KeyValue keyValue;
KeyValueBlobReader::State state;
while ((state = reader.Next(&keyValue)) == KeyValueBlobReader::State::HAS_PAIR) {
while (reader.Next(&keyValue) == KeyValueBlobReader::State::HAS_PAIR) {
keyValueMap[keyValue.key] = keyValue.value;
}
return true;
Expand Down
3 changes: 1 addition & 2 deletions src/Engine/UserPhrasesLM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ bool UserPhrasesLM::open(const char* path)

KeyValueBlobReader reader(static_cast<char*>(data), length);
KeyValueBlobReader::KeyValue keyValue;
KeyValueBlobReader::State state;
while ((state = reader.Next(&keyValue)) == KeyValueBlobReader::State::HAS_PAIR) {
while (reader.Next(&keyValue) == KeyValueBlobReader::State::HAS_PAIR) {
// We invert the key and value, since in user phrases, "key" is the phrase value, and "value" is the BPMF reading.
keyRowMap[keyValue.value].emplace_back(keyValue.value, keyValue.key);
}
Expand Down
4 changes: 2 additions & 2 deletions src/KeyHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ bool KeyHandler::handle(Key key, McBopomofo::InputState* state,
const ErrorCallback& errorCallback) {
// From Key's definition, if shiftPressed is true, it can't be a simple key
// that can be represented by ASCII.
char simpleAscii = (key.ctrlPressed || key.shiftPressed) ? 0 : key.ascii;
char simpleAscii = (key.ctrlPressed || key.shiftPressed) ? '\0' : key.ascii;

// See if it's valid BPMF reading.
bool keyConsumedByReading = false;
Expand Down Expand Up @@ -424,7 +424,7 @@ void KeyHandler::candidatePanelCancelled(const StateCallback& stateCallback) {
stateCallback(buildInputtingState());
}

bool KeyHandler::handleCandidateKeyForTraditionalBompomofoIfRequired(
bool KeyHandler::handleCandidateKeyForTraditionalBopomofoIfRequired(
Key key,
const SelectCurrentCandidateCallback& SelectCurrentCandidateCallback,
const StateCallback& stateCallback, const ErrorCallback& errorCallback) {
Expand Down
4 changes: 2 additions & 2 deletions src/KeyHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class KeyHandler {
void candidatePanelCancelled(const StateCallback& stateCallback);

// Workaround for the Traditional Bopomofo mode.
bool handleCandidateKeyForTraditionalBompomofoIfRequired(
bool handleCandidateKeyForTraditionalBopomofoIfRequired(
Key key,
const SelectCurrentCandidateCallback& SelectCurrentCandidateCallback,
const StateCallback& stateCallback, const ErrorCallback& errorCallback);
Expand All @@ -103,7 +103,7 @@ class KeyHandler {
// Sets if we should move cursor after selection.
void setMoveCursorAfterSelection(bool flag);

// Sets if we should put lowercasesd letters into the composing buffer.
// Sets if we should put lowercased letters into the composing buffer.
void setPutLowercaseLettersToComposingBuffer(bool flag);

/// Sets if the ESC key clears entire composing buffer.
Expand Down
1 change: 1 addition & 0 deletions src/LanguageModelLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <filesystem>
#include <fstream>
#include <memory>
#include <utility>

#include "Log.h"

Expand Down
30 changes: 17 additions & 13 deletions src/McBopomofo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ McBopomofoEngine::McBopomofoEngine(fcitx::Instance* instance)
"mcbopomofo-user-excluded-phrases-edit", excludedPhrasesAction_.get());

// Required by convention of fcitx5 modules to load config on its own.
// NOLINTNEXTLINE(clang-analyzer-optin.cplusplus.VirtualCall)
reloadConfig();
}

Expand Down Expand Up @@ -293,6 +294,7 @@ void McBopomofoEngine::activate(const fcitx::InputMethodEntry& entry,

keyHandler_->setInputMode(mode);

// NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
auto layout = Formosa::Mandarin::BopomofoKeyboardLayout::StandardLayout();
switch (config_.bopomofoKeyboardLayout.value()) {
case BopomofoKeyboardLayout::Standard:
Expand Down Expand Up @@ -541,20 +543,19 @@ void McBopomofoEngine::handleCandidateKeyEvent(
}
}

bool result =
keyHandler_->handleCandidateKeyForTraditionalBompomofoIfRequired(
MapFcitxKey(key),
[candidateList, context] {
auto idx = candidateList->cursorIndex();
if (idx < candidateList->size()) {
bool result = keyHandler_->handleCandidateKeyForTraditionalBopomofoIfRequired(
MapFcitxKey(key),
[candidateList, context] {
auto idx = candidateList->cursorIndex();
if (idx < candidateList->size()) {
#ifdef USE_LEGACY_FCITX5_API
candidateList->candidate(idx)->select(context);
candidateList->candidate(idx)->select(context);
#else
candidateList->candidate(idx).select(context);
candidateList->candidate(idx).select(context);
#endif
}
},
stateCallback, errorCallback);
}
},
stateCallback, errorCallback);

if (result) {
return;
Expand Down Expand Up @@ -676,7 +677,7 @@ void McBopomofoEngine::handleCandidatesState(
}

candidateList->setSelectionKey(selectionKeys_);
candidateList->setPageSize(selectionKeys_.size());
candidateList->setPageSize(static_cast<int>(selectionKeys_.size()));

fcitx::CandidateLayoutHint layoutHint = getCandidateLayoutHint();
candidateList->setLayoutHint(layoutHint);
Expand Down Expand Up @@ -772,9 +773,12 @@ void McBopomofoEngine::updatePreedit(fcitx::InputContext* context,
preedit.append(marking->markedText, fcitx::TextFormatFlag::HighLight);
preedit.append(marking->tail, normalFormat);
} else {
// Note: dynamic_cast<InputStates::Marking*> in enterNewState() ensures
// state is not nullptr
// NOLINTNEXTLINE(clang-analyzer-core.NonNullParamChecker)
preedit.append(state->composingBuffer, normalFormat);
}
preedit.setCursor(state->cursorIndex);
preedit.setCursor(static_cast<int>(state->cursorIndex));

if (useClientPreedit) {
context->inputPanel().setClientPreedit(preedit);
Expand Down

0 comments on commit 6de44eb

Please sign in to comment.