Hi,
I am a new developer who would like to contribute . I have created the
following patch which fixes issue Bug 5385 -fixit for missing includes.
-que
http://reviews.llvm.org/D5770
---
lib/Sema/SemaDecl.cpp | 40
++++++++++++++++++++++++++++--
test/Sema/Inputs/dummyheader.h | 1 +
test/Sema/implicit-builtin-decl.c | 2 +-
test/Sema/implicit-builtin-include-fixit.c | 10 ++++++++
4 files changed, 50 insertions(+), 3 deletions(-)
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 22a7e73..9755f25 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -1683,15 +1683,51 @@ NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID,
return nullptr;
}
+ //get the file buffer
+ FileID FileID = SourceMgr.getFileID(Loc);
+ llvm::MemoryBuffer* FileBuf = SourceMgr.getBuffer(FileID);
+ SourceLocation LastIncludeLoc = SourceMgr.getLocForStartOfFile(FileID);
+
+ //Find the Location of the last #include directive in the file
+ Lexer IncludeLexer(FileID,FileBuf,SourceMgr,LangOpts);
+ while (1) {
+ Token Tok;
+ IncludeLexer.LexFromRawLexer(Tok);
+ if (Tok.is(tok::eof))
+ break;
+ if (Tok.is(tok::hash)){
+ IncludeLexer.LexFromRawLexer(Tok);
+ if(Tok.getRawIdentifier() == "include" && Tok.getLocation().isValid())
+ LastIncludeLoc = Tok.getLocation();
+ }
+ }
+
+
+ //Get the location of the line after the last #include directive
+ PresumedLoc PLoc = SourceMgr.getPresumedLoc(LastIncludeLoc);
+ SourceLocation FixitLoc = SourceMgr.translateLineCol(FileID,PLoc.getLine()+1,PLoc.getColumn());
+ if (FixitLoc.isInvalid()){
+ FixitLoc = LastIncludeLoc;
+ }
+
+
+
if (!ForRedeclaration && Context.BuiltinInfo.isPredefinedLibFunction(ID)) {
Diag(Loc, diag::ext_implicit_lib_function_decl)
<< Context.BuiltinInfo.GetName(ID)
<< R;
+ //Build 'insert missing header' string
+ std::string FixitString = "#include <";
+ FixitString.append(Context.BuiltinInfo.getHeaderName(ID));
+ FixitString.append(">");
+ StringRef FixitStringRef(FixitString.c_str());
+
if (Context.BuiltinInfo.getHeaderName(ID) &&
!Diags.isIgnored(diag::ext_implicit_lib_function_decl, Loc))
- Diag(Loc, diag::note_include_header_or_declare)
+ Diag(FixitLoc, diag::note_include_header_or_declare)
<< Context.BuiltinInfo.getHeaderName(ID)
- << Context.BuiltinInfo.GetName(ID);
+ << Context.BuiltinInfo.GetName(ID)
+ << FixItHint::CreateInsertion(FixitLoc, FixitStringRef);
}
DeclContext *Parent = Context.getTranslationUnitDecl();
diff --git a/test/Sema/Inputs/dummyheader.h b/test/Sema/Inputs/dummyheader.h
new file mode 100644
index 0000000..694d9ae
--- /dev/null
+++ b/test/Sema/Inputs/dummyheader.h
@@ -0,0 +1 @@
+//this is a dummy header used by implicit-builtin-include-fixit.c
diff --git a/test/Sema/implicit-builtin-decl.c b/test/Sema/implicit-builtin-decl.c
index 3c2ff94..b1b8c23 100644
--- a/test/Sema/implicit-builtin-decl.c
+++ b/test/Sema/implicit-builtin-decl.c
@@ -3,7 +3,7 @@
void f() {
int *ptr = malloc(sizeof(int) * 10); // expected-warning{{implicitly declaring library function 'malloc' with type}} \
- // expected-note{{include the header <stdlib.h> or explicitly provide a declaration for 'malloc'}} \
+ // expected-note@-3{{include the header <stdlib.h> or explicitly provide a declaration for 'malloc'}} \
// expected-note{{'malloc' is a builtin with type 'void *}}
}
diff --git a/test/Sema/implicit-builtin-include-fixit.c b/test/Sema/implicit-builtin-include-fixit.c
new file mode 100644
index 0000000..d8155a7
--- /dev/null
+++ b/test/Sema/implicit-builtin-include-fixit.c
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wdocumentation -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
+
+#include "Inputs/dummyheader.h"/*dummy header to test implicit declaration fixit*/
+
+void f() {
+ int *ptr = malloc(sizeof(int) * 10); // expected-warning{{implicitly declaring library function 'malloc' with type}}\
+ // expected-note@-2{{include the header <stdlib.h> or explicitly provide a declaration for 'malloc'}}
+ // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:1-[[@LINE-4]]:1}:"#include <stdlib.h>"
+}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits