Index: /trunk/src/bldprogs/gccplugin.cpp
===================================================================
--- /trunk/src/bldprogs/gccplugin.cpp	(revision 56932)
+++ /trunk/src/bldprogs/gccplugin.cpp	(revision 56933)
@@ -27,4 +27,5 @@
 #include "tree.h"
 #include "tree-pass.h"
+#include "cp/cp-tree.h"
 
 
@@ -39,6 +40,7 @@
 *   Defined Constants And Macros                                                                                                 *
 *********************************************************************************************************************************/
+#define GCCPLUGIN_DEBUG
 /** Debug printf. */
-#if 1
+#ifdef GCCPLUGIN_DEBUG
 # define dprintf(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
 #else
@@ -47,4 +49,23 @@
 
 
+/** For use with messages. */
+#define MY_LOC(a_hPreferred, a_pState) \
+    (DECL_P(a_hPreferred) ? DECL_SOURCE_LOCATION(a_hPreferred) : gimple_location((a_pState)->hStmt))
+
+
+/*******************************************************************************
+*   Structures and Typedefs                                                    *
+*******************************************************************************/
+/** Checker state. */
+typedef struct MYCHECKSTATE
+{
+    gimple      hStmt;
+    long        iFmt;
+    long        iArgs;
+} MYCHECKSTATE;
+/** Pointer to my checker state. */
+typedef MYCHECKSTATE *PMYCHECKSTATE;
+
+
 /*********************************************************************************************************************************
 *   Internal Functions                                                                                                           *
@@ -71,5 +92,5 @@
     {
         .type                   = GIMPLE_PASS,
-        .name                   = "iprt-format-checks",
+        .name                   = "*iprt-format-checks", /* asterisk = no dump */
         .optinfo_flags          = 0,
         .gate                   = MyPassGateCallback,
@@ -93,5 +114,5 @@
     .reference_pass_name        = "ssa",
     .ref_pass_instance_number   = 1,
-    .pos_op                     = PASS_POS_INSERT_AFTER,
+    .pos_op                     = PASS_POS_INSERT_BEFORE,
 };
 
@@ -114,4 +135,53 @@
 
 
+#ifdef GCCPLUGIN_DEBUG
+
+/**
+ * Debug function for printing the scope of a decl.
+ * @param   hDecl               Declaration to print scope for.
+ */
+static void dprintScope(tree hDecl)
+{
+    tree hScope = CP_DECL_CONTEXT(hDecl);
+    if (hScope == global_namespace)
+        return;
+    if (TREE_CODE(hScope) == RECORD_TYPE)
+        hScope = TYPE_NAME(hScope);
+
+    /* recurse */
+    dprintScope(hScope);
+
+    /* name the scope. */
+    dprintf("::%s", DECL_NAME(hScope) ? IDENTIFIER_POINTER(DECL_NAME(hScope)) : "<noname>");
+}
+
+
+/**
+ * Debug function for printing a declaration.
+ * @param   hDecl               The declaration to print.
+ */
+static void dprintDecl(tree hDecl)
+{
+    enum tree_code const    enmDeclCode = TREE_CODE(hDecl);
+    tree const              hType       = TREE_TYPE(hDecl);
+    enum tree_code const    enmTypeCode = hType ? TREE_CODE(hType) : (enum tree_code)-1;
+#if 0
+    if (   enmTypeCode == RECORD_TYPE
+        && enmDeclCode == TYPE_DECL
+        && DECL_ARTIFICIAL(hDecl))
+        dprint_class(hType);
+#endif
+
+    dprintf("%s ", tree_code_name[enmDeclCode]);
+    dprintScope(hDecl);
+    dprintf("::%s", DECL_NAME(hDecl) ? IDENTIFIER_POINTER(DECL_NAME(hDecl)) : "<noname>");
+    if (hType)
+        dprintf(" type %s", tree_code_name[enmTypeCode]);
+    dprintf(" @%s:%d", DECL_SOURCE_FILE(hDecl),  DECL_SOURCE_LINE(hDecl));
+}
+
+#endif /* GCCPLUGIN_DEBUG */
+
+
 /**
  * Gate callback for my pass that indicates whether it should execute or not.
@@ -125,4 +195,31 @@
 
 
+static void MyCheckFormatWorker(PMYCHECKSTATE pState, tree hFmtArg)
+{
+
+}
+
+
+/**
+ * Deal recursively with special format string constructs.
+ *
+ * This will call MyCheckFormatWorker to validate each format string.
+ *
+ * @param   pState              The format string check state.
+ * @param   hFmtArg             The format string node.
+ */
+static void MyCheckFormatRecursive(PMYCHECKSTATE pState, tree hFmtArg)
+{
+    if (integer_zerop(hFmtArg))
+        warning_at(MY_LOC(hFmtArg, pState), 0, "Format string should not be NULL");
+    else
+    {
+        /** @todo more to do here. ternary expression, ++. */
+
+        MyCheckFormatWorker(pState, hFmtArg);
+    }
+}
+
+
 /**
  * Execute my pass.
@@ -133,4 +230,7 @@
     dprintf("MyPassExecuteCallback:\n");
 
+    /*
+     * Enumerate the basic blocks.
+     */
     basic_block hBasicBlock;
     FOR_EACH_BB(hBasicBlock)
@@ -138,9 +238,58 @@
         dprintf(" hBasicBlock=%p\n", hBasicBlock);
 
-        for (gimple_stmt_iterator hStmtItr = gsi_start_bb(hBasicBlock); gsi_end_p(hStmtItr); gsi_next(&hStmtItr))
+        /*
+         * Enumerate the statements in the current basic block.
+         * We're interested in calls to functions with the __iprt_format__ attribute.
+         */
+        for (gimple_stmt_iterator hStmtItr = gsi_start_bb(hBasicBlock); !gsi_end_p(hStmtItr); gsi_next(&hStmtItr))
         {
-            gimple hStmt = gsi_stmt(hStmtItr);
-            dprintf(" hStmt=%p ops=%d\n", hStmt, gimple_num_ops(hStmt));
-
+            gimple const            hStmt   = gsi_stmt(hStmtItr);
+            enum gimple_code const  enmCode = gimple_code(hStmt);
+#ifdef GCCPLUGIN_DEBUG
+            unsigned const          cOps    = gimple_num_ops(hStmt);
+            dprintf("   hStmt=%p %s (%d) ops=%d\n", hStmt, gimple_code_name[enmCode], enmCode, cOps);
+            for (unsigned iOp = 0; iOp < cOps; iOp++)
+            {
+                tree const hOp = gimple_op(hStmt, iOp);
+                if (hOp)
+                    dprintf("     %02d: %p, code %s(%d)\n", iOp, hOp, tree_code_name[TREE_CODE(hOp)], TREE_CODE(hOp));
+                else
+                    dprintf("     %02d: NULL_TREE\n", iOp);
+            }
+#endif
+            if (enmCode == GIMPLE_CALL)
+            {
+                /*
+                 * Check if the function type has the __iprt_format__ attribute.
+                 */
+                tree const hFn       = gimple_call_fn(hStmt);
+                tree const hFnType   = gimple_call_fntype(hStmt);
+                tree const hAttr     = lookup_attribute("iprt_format", TYPE_ATTRIBUTES(hFnType));
+#ifdef GCCPLUGIN_DEBUG
+                tree const hFnDecl   = gimple_call_fndecl(hStmt);
+                dprintf("     hFn    =%p %s(%d); args=%d\n",
+                        hFn, tree_code_name[TREE_CODE(hFn)], TREE_CODE(hFn), gimple_call_num_args(hStmt));
+                if (hFnDecl)
+                    dprintf("     hFnDecl=%p %s(%d) type=%p %s:%d\n", hFnDecl, tree_code_name[TREE_CODE(hFnDecl)],
+                            TREE_CODE(hFnDecl), TREE_TYPE(hFnDecl), DECL_SOURCE_FILE(hFnDecl), DECL_SOURCE_LINE(hFnDecl));
+                if (hFnType)
+                    dprintf("     hFnType=%p %s(%d)\n", hFnType, tree_code_name[TREE_CODE(hFnType)], TREE_CODE(hFnType));
+#endif
+                if (hAttr)
+                {
+                    /*
+                     * Yeah, it has the attribute!
+                     */
+                    tree const hAttrArgs = TREE_VALUE(hAttr);
+                    MYCHECKSTATE State;
+                    State.iFmt  = TREE_INT_CST(TREE_VALUE(hAttrArgs)).to_shwi();
+                    State.iArgs = TREE_INT_CST(TREE_VALUE(TREE_CHAIN(hAttrArgs))).to_shwi();
+                    State.hStmt = hStmt;
+                    dprintf("     %s() __iprt_format__(iFmt=%ld, iArgs=%ld)\n",
+                            DECL_NAME(hFnDecl) ? IDENTIFIER_POINTER(DECL_NAME(hFnDecl)) : "<unamed>", State.iFmt, State.iArgs);
+
+                    MyCheckFormatRecursive(&State, gimple_call_arg(hStmt, State.iFmt - 1));
+                }
+            }
         }
     }
@@ -162,10 +311,11 @@
 static tree AttributeHandler(tree *phOnNode, tree hAttrName, tree hAttrArgs, int fFlags, bool *pfDontAddAttrib)
 {
-    dprintf("AttributeHandler: name=%s fFlags=%#x ", IDENTIFIER_POINTER(hAttrName), fFlags);
+    dprintf("AttributeHandler: name=%s fFlags=%#x", IDENTIFIER_POINTER(hAttrName), fFlags);
     long iFmt  = TREE_INT_CST(TREE_VALUE(hAttrArgs)).to_shwi();
     long iArgs = TREE_INT_CST(TREE_VALUE(TREE_CHAIN(hAttrArgs))).to_shwi();
-    dprintf("iFmt=%ld iArgs=%ld\n", iFmt, iArgs);
+    dprintf(" iFmt=%ld iArgs=%ld", iFmt, iArgs);
 
     tree hType = *phOnNode;
+    dprintf(" hType=%p %s(%d)\n", hType, tree_code_name[TREE_CODE(hType)], TREE_CODE(hType));
 
     if (pfDontAddAttrib)
Index: /trunk/src/bldprogs/test-gccplugin.c
===================================================================
--- /trunk/src/bldprogs/test-gccplugin.c	(revision 56933)
+++ /trunk/src/bldprogs/test-gccplugin.c	(revision 56933)
@@ -0,0 +1,10 @@
+
+extern void MyIprtPrintf(const char *pszFormat, ...) __attribute__((__iprt_format__(1,2)));
+extern void foo(void);
+
+void foo(void)
+{
+    MyIprtPrintf(0);
+    MyIprtPrintf("%RX32 %d %s\n", 10, 42, "string");
+}
+
