From patchwork Fri May 9 15:56:01 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 29910 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ob0-f197.google.com (mail-ob0-f197.google.com [209.85.214.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 1467E20534 for ; Fri, 9 May 2014 15:56:07 +0000 (UTC) Received: by mail-ob0-f197.google.com with SMTP id vb8sf20741231obc.0 for ; Fri, 09 May 2014 08:56:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=f+XvGKh1XxD6unltO4zr0c4qGJYmRi+ngFIbgmw5c64=; b=QjTQhx7y8Z07kFdkEtjl/7DBOzgkpBKUNo7UW6bbQUlqyasPE6mOLWIlmCxGwycjkz RxENVSVQSI2d/gq9i55GXHsJLqFt/gsHMHgpPayvfmK6wdTD8XHbTCYYsXwZMDEyColD 62nSp4Nwq0LuCblL4eWTY6N+vYAPgYvpQHUCD7weQzGkchB350EgpvpfTyHT5FDqUXtU rahk3bfDj5L0Zbru775VTqxuOXulD9eSVa2OokEGGbMTLhyFavpUmhamJxJxxtAVrK+M 8pfpgFSMmq3+m9f5Wa9hosfHa8gJ6K5u7E8tz5xggrZfnDpbPZR9BNwXLgfE1C5GtsVN 8Nbg== X-Gm-Message-State: ALoCoQmvpz3IcQ5fmVQZaw9+orzZXUPPpqaY8AiYVjCXquU7MdaXoSVoSLADsgqmOko8+6Rp+ZZr X-Received: by 10.182.186.103 with SMTP id fj7mr5506541obc.9.1399650967633; Fri, 09 May 2014 08:56:07 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.86.100 with SMTP id o91ls381416qgd.30.gmail; Fri, 09 May 2014 08:56:07 -0700 (PDT) X-Received: by 10.220.92.135 with SMTP id r7mr9072913vcm.11.1399650967538; Fri, 09 May 2014 08:56:07 -0700 (PDT) Received: from mail-ve0-f181.google.com (mail-ve0-f181.google.com [209.85.128.181]) by mx.google.com with ESMTPS id ko12si795124veb.27.2014.05.09.08.56.07 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 May 2014 08:56:07 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.128.181 as permitted sender) client-ip=209.85.128.181; Received: by mail-ve0-f181.google.com with SMTP id pa12so5460842veb.12 for ; Fri, 09 May 2014 08:56:07 -0700 (PDT) X-Received: by 10.58.179.115 with SMTP id df19mr1195589vec.41.1399650967433; Fri, 09 May 2014 08:56:07 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.221.72 with SMTP id ib8csp92033vcb; Fri, 9 May 2014 08:56:06 -0700 (PDT) X-Received: by 10.180.184.167 with SMTP id ev7mr3886864wic.55.1399650966612; Fri, 09 May 2014 08:56:06 -0700 (PDT) Received: from mnementh.archaic.org.uk (mnementh.archaic.org.uk. [2001:8b0:1d0::1]) by mx.google.com with ESMTPS id g5si1747915wjw.110.2014.05.09.08.56.06 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Fri, 09 May 2014 08:56:06 -0700 (PDT) Received-SPF: none (google.com: pm215@archaic.org.uk does not designate permitted sender hosts) client-ip=2001:8b0:1d0::1; Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.80) (envelope-from ) id 1Win9Y-0005UJ-CZ; Fri, 09 May 2014 16:56:04 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Cc: patches@linaro.org, Anthony Liguori , =?UTF-8?q?Andreas=20F=C3=A4rber?= Subject: [PATCH v2 1/4] Provide infrastructure for marking private QOM struct fields Date: Fri, 9 May 2014 16:56:01 +0100 Message-Id: <1399650964-21067-2-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1399650964-21067-1-git-send-email-peter.maydell@linaro.org> References: <1399650964-21067-1-git-send-email-peter.maydell@linaro.org> X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: peter.maydell@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.128.181 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Provide infrastructure for marking private QOM struct fields, so that a compiler warning is generated when a user of the QOM object attempts to access them directly. This is implemented using GCC's 'deprecated' attribute; preprocessor macros arrange that when compiling the class implementation, no attribute is applied to the fields; when compiling a user of the class the fields are marked deprecated. This allows us to have a single simple C struct defining the object, and for users of the QOM object to be able to embed instances of it into other structs, but still to have a guard against users accidentally touching parts of the structure they should not be accessing. Signed-off-by: Peter Maydell --- include/qemu/compiler.h | 10 ++++++++++ include/qom/object.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h index 155b358..d7cc153 100644 --- a/include/qemu/compiler.h +++ b/include/qemu/compiler.h @@ -52,4 +52,14 @@ #define GCC_FMT_ATTR(n, m) #endif +/* An attribute usable to mark structure fields as private to the + * implementation; since this is only a diagnostic to catch programming + * errors, it's OK if it expands to nothing on non-gcc compilers. + */ +#if defined __GNUC__ +# define QEMU_PRIVATE_ATTR __attribute__((deprecated("this field is private"))) +#else +# define QEMU_PRIVATE_ATTR +#endif + #endif /* COMPILER_H */ diff --git a/include/qom/object.h b/include/qom/object.h index a641dcd..ea80008 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -284,6 +284,53 @@ typedef struct InterfaceInfo InterfaceInfo; * * The first example of such a QOM method was #CPUClass.reset, * another example is #DeviceClass.realize. + * + * = Marking fields as private to the class implementation = + * + * The expected code structure for QOM objects is that they should + * have a header file in include/ which defines the class and object + * structures and the typecasting macros. This header can then be + * included by both the source file which implements the QOM object + * and also by other source files which merely wish to use the object. + * Users of your object need the class and object structures so that + * they can embed instances of the object in their own structures; + * however they do not need to be able to access individual fields in + * these structures. To enforce this you should use the QEMU_PRIVATE_ATTR + * macro in a pattern like this: + * + * + * Marking fields as private + * + * #ifdef IMPLEMENTING_MY_DEVICE + * # define qom_private + * #else + * # define qom_private QEMU_PRIVATE_ATTR + * #endif + * + * typedef struct MyDevice + * { + * qom_private DeviceState parent; + * + * qom_private int reg0, reg1, reg2; + * } MyDevice; + * + * typedef struct MyDeviceClass + * { + * qom_private DeviceClass parent; + * + * void (*frobnicate) (MyDevice *obj); + * } MyDeviceClass; + * + * #undef qom_private + * + * + * + * The source files which provide the implementation of your + * class (or of subclasses to it) should then have + * "#define IMPLEMENTING_MY_DEVICE" before they include any + * headers. Since users of the class will not define this + * macro, they will get a compilation warning if they access + * any of the private fields by mistake. */