Fix clang-tidy errors in sounddose directory

Test: atest sounddosemanager_test
Bug: 286466288
Change-Id: I0b4ed24ed28ad423f82dd1db8813d2aff31bef9d
Merged-In: I0b4ed24ed28ad423f82dd1db8813d2aff31bef9d
(cherry picked from commit f2cf6747297a777ea3fb7564b358c8b1e4568cda)
diff --git a/services/audioflinger/sounddose/Android.bp b/services/audioflinger/sounddose/Android.bp
index 0a8c8be..d659261 100644
--- a/services/audioflinger/sounddose/Android.bp
+++ b/services/audioflinger/sounddose/Android.bp
@@ -7,12 +7,109 @@
     default_applicable_licenses: ["frameworks_av_services_audioflinger_license"],
 }
 
+audioflinger_sounddose_tidy_errors = [
+    // https://clang.llvm.org/extra/clang-tidy/checks/list.html
+    // For many categories, the checks are too many to specify individually.
+    // Feel free to disable as needed - as warnings are generally ignored,
+    // we treat warnings as errors.
+    "android-*",
+    "bugprone-*",
+    "cert-*",
+    "clang-analyzer-security*",
+    "google-*",
+    "misc-*",
+    //"modernize-*",  // explicitly list the modernize as they can be subjective.
+    "modernize-avoid-bind",
+    "modernize-avoid-c-arrays", // std::array<> can be verbose
+    "modernize-concat-nested-namespaces",
+    "modernize-deprecated-headers", // C headers still ok even if there is C++ equivalent.
+    "modernize-deprecated-ios-base-aliases",
+    "modernize-loop-convert",
+    "modernize-make-shared",
+    "modernize-make-unique",
+    "modernize-pass-by-value",
+    "modernize-raw-string-literal",
+    "modernize-redundant-void-arg",
+    "modernize-replace-auto-ptr",
+    "modernize-replace-random-shuffle",
+    "modernize-return-braced-init-list",
+    "modernize-shrink-to-fit",
+    "modernize-unary-static-assert",
+    "modernize-use-auto",  // debatable - auto can obscure type
+    "modernize-use-bool-literals",
+    "modernize-use-default-member-init",
+    "modernize-use-emplace",
+    "modernize-use-equals-default",
+    "modernize-use-equals-delete",
+    "modernize-use-nodiscard",
+    "modernize-use-noexcept",
+    "modernize-use-nullptr",
+    "modernize-use-override",
+    // "modernize-use-trailing-return-type", // not necessarily more readable
+    "modernize-use-transparent-functors",
+    "modernize-use-uncaught-exceptions",
+    "modernize-use-using",
+    "performance-*",
+
+    // Remove some pedantic stylistic requirements.
+    "-google-readability-casting", // C++ casts not always necessary and may be verbose
+    "-google-readability-todo",    // do not require TODO(info)
+
+    "-bugprone-unhandled-self-assignment",
+    "-bugprone-suspicious-string-compare",
+    "-cert-oop54-cpp", // found in TransactionLog.h
+    "-bugprone-narrowing-conversions", // b/182410845
+
+    "-misc-non-private-member-variables-in-classes",
+]
+
+// Eventually use common tidy defaults
+cc_defaults {
+    name: "audioflinger_sounddose_flags_defaults",
+    // https://clang.llvm.org/docs/UsersManual.html#command-line-options
+    // https://clang.llvm.org/docs/DiagnosticsReference.html
+    cflags: [
+        "-Wall",
+        "-Wdeprecated",
+        "-Werror",
+        "-Werror=implicit-fallthrough",
+        "-Werror=sometimes-uninitialized",
+        "-Werror=conditional-uninitialized",
+        "-Wextra",
+
+        // suppress some warning chatter.
+        "-Wno-deprecated-copy-with-dtor",
+        "-Wno-deprecated-copy-with-user-provided-dtor",
+
+        "-Wredundant-decls",
+        "-Wshadow",
+        "-Wstrict-aliasing",
+        "-fstrict-aliasing",
+        "-Wthread-safety",
+        //"-Wthread-safety-negative", // experimental - looks broken in R.
+        "-Wunreachable-code",
+        "-Wunreachable-code-break",
+        "-Wunreachable-code-return",
+        "-Wunused",
+        "-Wused-but-marked-unused",
+        "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
+    ],
+    // https://clang.llvm.org/extra/clang-tidy/
+    tidy: true,
+    tidy_checks: audioflinger_sounddose_tidy_errors,
+    tidy_checks_as_errors: audioflinger_sounddose_tidy_errors,
+    tidy_flags: [
+      "-format-style=file",
+    ],
+}
+
 cc_library {
     name: "libsounddose",
 
     double_loadable: true,
 
     defaults: [
+        "audioflinger_sounddose_flags_defaults",
         "latest_android_media_audio_common_types_ndk_shared",
         "latest_android_hardware_audio_core_sounddose_ndk_shared",
         "latest_android_hardware_audio_sounddose_ndk_shared",
diff --git a/services/audioflinger/sounddose/SoundDoseManager.cpp b/services/audioflinger/sounddose/SoundDoseManager.cpp
index a114a38..21f346e 100644
--- a/services/audioflinger/sounddose/SoundDoseManager.cpp
+++ b/services/audioflinger/sounddose/SoundDoseManager.cpp
@@ -30,7 +30,6 @@
 namespace android {
 
 using aidl::android::media::audio::common::AudioDevice;
-using aidl::android::media::audio::common::AudioDeviceAddress;
 
 namespace {
 
@@ -48,7 +47,7 @@
 sp<audio_utils::MelProcessor> SoundDoseManager::getOrCreateProcessorForDevice(
         audio_port_handle_t deviceId, audio_io_handle_t streamHandle, uint32_t sampleRate,
         size_t channelCount, audio_format_t format) {
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
 
     if (mHalSoundDose != nullptr && mEnabledCsd) {
         ALOGD("%s: using HAL MEL computation, no MelProcessor needed.", __func__);
@@ -86,8 +85,9 @@
 bool SoundDoseManager::setHalSoundDoseInterface(const std::shared_ptr<ISoundDose>& halSoundDose) {
     ALOGV("%s", __func__);
 
+    std::shared_ptr<HalSoundDoseCallback> halSoundDoseCallback;
     {
-        std::lock_guard _l(mLock);
+        const std::lock_guard _l(mLock);
 
         mHalSoundDose = halSoundDose;
         if (halSoundDose == nullptr) {
@@ -106,9 +106,11 @@
             mHalSoundDoseCallback =
                 ndk::SharedRefBase::make<HalSoundDoseCallback>(this);
         }
+        halSoundDoseCallback = mHalSoundDoseCallback;
     }
 
-    auto status = halSoundDose->registerSoundDoseCallback(mHalSoundDoseCallback);
+    auto status = halSoundDose->registerSoundDoseCallback(halSoundDoseCallback);
+
     if (!status.isOk()) {
         // Not a warning since this can happen if the callback was registered before
         ALOGI("%s: Cannot register HAL sound dose callback with status message: %s",
@@ -121,7 +123,7 @@
 
 void SoundDoseManager::setOutputRs2UpperBound(float rs2Value) {
     ALOGV("%s", __func__);
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
 
     if (mHalSoundDose != nullptr) {
         // using the HAL sound dose interface
@@ -134,9 +136,9 @@
     }
 
     for (auto& streamProcessor : mActiveProcessors) {
-        sp<audio_utils::MelProcessor> processor = streamProcessor.second.promote();
+        const sp<audio_utils::MelProcessor> processor = streamProcessor.second.promote();
         if (processor != nullptr) {
-            status_t result = processor->setOutputRs2UpperBound(rs2Value);
+            const status_t result = processor->setOutputRs2UpperBound(rs2Value);
             if (result != NO_ERROR) {
                 ALOGW("%s: could not set RS2 upper bound %f for stream %d", __func__, rs2Value,
                       streamProcessor.first);
@@ -148,7 +150,7 @@
 }
 
 void SoundDoseManager::removeStreamProcessor(audio_io_handle_t streamHandle) {
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
     auto callbackToRemove = mActiveProcessors.find(streamHandle);
     if (callbackToRemove != mActiveProcessors.end()) {
         mActiveProcessors.erase(callbackToRemove);
@@ -156,7 +158,7 @@
 }
 
 audio_port_handle_t SoundDoseManager::getIdForAudioDevice(const AudioDevice& audioDevice) const {
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
 
     audio_devices_t type;
     std::string address;
@@ -178,14 +180,14 @@
 
 void SoundDoseManager::mapAddressToDeviceId(const AudioDeviceTypeAddr& adt,
                                             const audio_port_handle_t deviceId) {
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
     ALOGI("%s: map address: %d to device id: %d", __func__, adt.mType, deviceId);
     mActiveDevices[adt] = deviceId;
     mActiveDeviceTypes[deviceId] = adt.mType;
 }
 
 void SoundDoseManager::clearMapDeviceIdEntries(audio_port_handle_t deviceId) {
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
     for (auto activeDevice = mActiveDevices.begin(); activeDevice != mActiveDevices.end();) {
         if (activeDevice->second == deviceId) {
             ALOGI("%s: clear mapping type: %d to deviceId: %d",
@@ -303,7 +305,7 @@
     ALOGV("%s", __func__);
     auto soundDoseManager = mSoundDoseManager.promote();
     if (soundDoseManager != nullptr) {
-        std::lock_guard _l(soundDoseManager->mLock);
+        const std::lock_guard _l(soundDoseManager->mLock);
         *value = soundDoseManager->mRs2UpperBound;
     }
     return binder::Status::ok();
@@ -348,7 +350,7 @@
 }
 
 void SoundDoseManager::updateAttenuation(float attenuationDB, audio_devices_t deviceType) {
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
     ALOGV("%s: updating MEL processor attenuation for device type %d to %f",
             __func__, deviceType, attenuationDB);
     mMelAttenuationDB[deviceType] = attenuationDB;
@@ -368,7 +370,7 @@
 void SoundDoseManager::setCsdEnabled(bool enabled) {
     ALOGV("%s",  __func__);
 
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
     mEnabledCsd = enabled;
 
     for (auto& activeEntry : mActiveProcessors) {
@@ -384,7 +386,7 @@
 }
 
 bool SoundDoseManager::isCsdEnabled() {
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
     return mEnabledCsd;
 }
 
@@ -392,52 +394,51 @@
     // invalidate any HAL sound dose interface used
     setHalSoundDoseInterface(nullptr);
 
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
     mUseFrameworkMel = useFrameworkMel;
 }
 
 bool SoundDoseManager::forceUseFrameworkMel() const {
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
     return mUseFrameworkMel;
 }
 
 void SoundDoseManager::setComputeCsdOnAllDevices(bool computeCsdOnAllDevices) {
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
     mComputeCsdOnAllDevices = computeCsdOnAllDevices;
 }
 
 bool SoundDoseManager::forceComputeCsdOnAllDevices() const {
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
     return mComputeCsdOnAllDevices;
 }
 
 bool SoundDoseManager::isSoundDoseHalSupported() const {
-    if (!mEnabledCsd) {
-        return false;
+    {
+        const std::lock_guard _l(mLock);
+        if (!mEnabledCsd) return false;
     }
 
     std::shared_ptr<ISoundDose> halSoundDose;
     getHalSoundDose(&halSoundDose);
-    if (mHalSoundDose == nullptr) {
-        return false;
-    }
-    return true;
+    return halSoundDose != nullptr;
 }
 
 void SoundDoseManager::getHalSoundDose(std::shared_ptr<ISoundDose>* halSoundDose) const {
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
     *halSoundDose = mHalSoundDose;
 }
 
 void SoundDoseManager::resetSoundDose() {
-    std::lock_guard lock(mLock);
+    const std::lock_guard lock(mLock);
     mSoundDose = nullptr;
 }
 
 void SoundDoseManager::resetCsd(float currentCsd,
                                 const std::vector<media::SoundDoseRecord>& records) {
-    std::lock_guard lock(mLock);
+    const std::lock_guard lock(mLock);
     std::vector<audio_utils::CsdRecord> resetRecords;
+    resetRecords.reserve(records.size());
     for (const auto& record : records) {
         resetRecords.emplace_back(record.timestamp, record.duration, record.value,
                                   record.averageMel);
@@ -455,13 +456,13 @@
     std::vector<audio_utils::CsdRecord> records;
     float currentCsd;
     {
-        std::lock_guard _l(mLock);
+        const std::lock_guard _l(mLock);
         if (!mEnabledCsd) {
             return;
         }
 
 
-        int64_t timestampSec = getMonotonicSecond();
+        const int64_t timestampSec = getMonotonicSecond();
 
         // only for internal callbacks
         records = mMelAggregator->aggregateAndAddNewMelRecord(audio_utils::MelRecord(
@@ -475,6 +476,7 @@
 
     if (records.size() > 0 && soundDoseCallback != nullptr) {
         std::vector<media::SoundDoseRecord> newRecordsToReport;
+        newRecordsToReport.resize(records.size());
         for (const auto& record : records) {
             newRecordsToReport.emplace_back(csdRecordToSoundDoseRecord(record));
         }
@@ -484,7 +486,7 @@
 }
 
 sp<media::ISoundDoseCallback> SoundDoseManager::getSoundDoseCallback() const {
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
     if (mSoundDose == nullptr) {
         return nullptr;
     }
@@ -496,7 +498,7 @@
     ALOGV("%s: Momentary exposure for device %d triggered: %f MEL", __func__, deviceId, currentMel);
 
     {
-        std::lock_guard _l(mLock);
+        const std::lock_guard _l(mLock);
         if (!mEnabledCsd) {
             return;
         }
@@ -512,7 +514,7 @@
         const sp<media::ISoundDoseCallback>& callback) {
     ALOGV("%s: Register ISoundDoseCallback", __func__);
 
-    std::lock_guard _l(mLock);
+    const std::lock_guard _l(mLock);
     if (mSoundDose == nullptr) {
         mSoundDose = sp<SoundDose>::make(this, callback);
     }
@@ -522,7 +524,7 @@
 std::string SoundDoseManager::dump() const {
     std::string output;
     {
-        std::lock_guard _l(mLock);
+        const std::lock_guard _l(mLock);
         if (!mEnabledCsd) {
             base::StringAppendF(&output, "CSD is disabled");
             return output;
diff --git a/services/audioflinger/sounddose/SoundDoseManager.h b/services/audioflinger/sounddose/SoundDoseManager.h
index 6c02afb..9ed0661 100644
--- a/services/audioflinger/sounddose/SoundDoseManager.h
+++ b/services/audioflinger/sounddose/SoundDoseManager.h
@@ -129,7 +129,7 @@
               mSoundDoseCallback(callback) {}
 
         /** IBinder::DeathRecipient. Listen to the death of ISoundDoseCallback. */
-        virtual void binderDied(const wp<IBinder>& who);
+        void binderDied(const wp<IBinder>& who) override;
 
         /** BnSoundDose override */
         binder::Status setOutputRs2UpperBound(float value) override;