From 282351bf126ca09cb5c3b9bf4663a52dc7f355e3 Mon Sep 17 00:00:00 2001 From: thakis Date: Thu, 7 May 2015 16:43:17 -0700 Subject: [PATCH] plugin: Don't warn about inline virtual methods from system macros; whitelist two non-system macros. Even though CR_BEGIN_MSG_MAP_EX and BEGIN_SAFE_MSG_MAP_EX are from chromium code, they are interface-compatible with the system macro BEGIN_MSG_MAP_EX and so we can't fix the style plugin warnings about them. So whitelist these two macros. BUG=484721 Review URL: https://codereview.chromium.org/1126413004 Cr-Commit-Position: refs/heads/master@{#328880} --- tools/clang/plugins/FindBadConstructsConsumer.cpp | 24 ++++++++++++++++++++--- tools/clang/plugins/tests/system/windows.h | 2 ++ tools/clang/plugins/tests/virtual_bodies.h | 10 ++++++++++ tools/clang/plugins/tests/virtual_bodies.txt | 2 +- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/tools/clang/plugins/FindBadConstructsConsumer.cpp b/tools/clang/plugins/FindBadConstructsConsumer.cpp index a7b75198f533..c03e8bf8d1d5 100644 --- a/tools/clang/plugins/FindBadConstructsConsumer.cpp +++ b/tools/clang/plugins/FindBadConstructsConsumer.cpp @@ -508,9 +508,27 @@ void FindBadConstructsConsumer::CheckVirtualBodies( if (method->hasBody() && method->hasInlineBody()) { if (CompoundStmt* cs = dyn_cast(method->getBody())) { if (cs->size()) { - emitWarning(cs->getLBracLoc(), - "virtual methods with non-empty bodies shouldn't be " - "declared inline."); + SourceLocation loc = cs->getLBracLoc(); + // CR_BEGIN_MSG_MAP_EX and BEGIN_SAFE_MSG_MAP_EX try to be compatible + // to BEGIN_MSG_MAP(_EX). So even though they are in chrome code, + // we can't easily fix them, so explicitly whitelist them here. + bool emit = true; + if (loc.isMacroID()) { + SourceManager& manager = instance().getSourceManager(); + if (InBannedDirectory(manager.getSpellingLoc(loc))) + emit = false; + else { + StringRef name = Lexer::getImmediateMacroName( + loc, manager, instance().getLangOpts()); + if (name == "CR_BEGIN_MSG_MAP_EX" || + name == "BEGIN_SAFE_MSG_MAP_EX") + emit = false; + } + } + if (emit) + emitWarning(loc, + "virtual methods with non-empty bodies shouldn't be " + "declared inline."); } } } diff --git a/tools/clang/plugins/tests/system/windows.h b/tools/clang/plugins/tests/system/windows.h index 66e923de45d5..98cc307c2795 100644 --- a/tools/clang/plugins/tests/system/windows.h +++ b/tools/clang/plugins/tests/system/windows.h @@ -12,4 +12,6 @@ #define SYSTEM_REDUNDANT1 virtual void NonVirtualFinal() final #define SYSTEM_REDUNDANT2 virtual void Virtual() override final +#define SYSTEM_INLINE_VIRTUAL virtual int Foo() { return 4; } + #endif // TOOLS_CLANG_PLUGINS_TESTS_SYSTEM_WINDOWS_H_ diff --git a/tools/clang/plugins/tests/virtual_bodies.h b/tools/clang/plugins/tests/virtual_bodies.h index 4ebe695dcbed..fb971c3f1e89 100644 --- a/tools/clang/plugins/tests/virtual_bodies.h +++ b/tools/clang/plugins/tests/virtual_bodies.h @@ -5,6 +5,12 @@ #ifndef VIRTUAL_METHODS_H_ #define VIRTUAL_METHODS_H_ +// Note: This is not actual windows.h but the stub file in system/windows.h +#include + +#define CR_BEGIN_MSG_MAP_EX(theClass) virtual int f() { return 4; } +#define BEGIN_SAFE_MSG_MAP_EX(theClass) virtual int g() { return 4; } + // Should warn about virtual method usage. class VirtualMethodsInHeaders { public: @@ -15,6 +21,10 @@ class VirtualMethodsInHeaders { // But complain about this: virtual bool ComplainAboutThis() { return true; } + + SYSTEM_INLINE_VIRTUAL + CR_BEGIN_MSG_MAP_EX(Sub) + BEGIN_SAFE_MSG_MAP_EX(Sub) }; // Complain on missing 'virtual' keyword in overrides. diff --git a/tools/clang/plugins/tests/virtual_bodies.txt b/tools/clang/plugins/tests/virtual_bodies.txt index 121d1a96aab5..c58f98e9d332 100644 --- a/tools/clang/plugins/tests/virtual_bodies.txt +++ b/tools/clang/plugins/tests/virtual_bodies.txt @@ -1,5 +1,5 @@ In file included from virtual_bodies.cpp:5: -./virtual_bodies.h:17:36: warning: [chromium-style] virtual methods with non-empty bodies shouldn't be declared inline. +./virtual_bodies.h:23:36: warning: [chromium-style] virtual methods with non-empty bodies shouldn't be declared inline. virtual bool ComplainAboutThis() { return true; } ^ 1 warning generated. -- 2.11.4.GIT